﻿Imports System.Globalization
Imports Microsoft.Office.Interop
Imports System.IO
Imports System.Runtime.InteropServices.Marshal
Imports System.Threading

Imports System
Imports System.Data
Imports System.Configuration
Imports System.Collections
Imports System.Web
Imports System.Web.Security
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts
Imports System.Web.UI.HtmlControls
Imports System.Text


Partial Public Class frm_ShipSchedule
    Inherits System.Web.UI.Page

    Dim clsGet As New ClassFunctionGetData
    Dim strHtml As StringBuilder = New StringBuilder()
    Dim strHtml1 As StringBuilder = New StringBuilder()

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim _scriptManager As ScriptManager = ScriptManager.GetCurrent(Me.Page)
        _scriptManager.RegisterPostBackControl(btnPrint)

        If Not (IsPostBack) Then
            Dim lbPage As Label = CType(Master.FindControl("lbPage"), Label)
            lbPage.Text = "SHIP SCHEDULE "

            Session("Ship_Booking") = Nothing
            'ondblclick
            'onclick

            ' txtDate.Text = Date.Now.AddDays(-7).ToString("dd MMM yyyy", New CultureInfo("en-US"))
            'txtDateTo.Text = Date.Now.AddDays(14).ToString("dd MMM yyyy", New CultureInfo("en-US"))

            txtDate.Text = Date.Now.AddDays(-1).ToString("dd MMM yyyy", New CultureInfo("en-US"))
            txtDateTo.Text = Date.Now.AddDays(3).ToString("dd MMM yyyy", New CultureInfo("en-US"))

            If (Session("Ship_View") Is Nothing) Then

            Else
                txtDate.Text = Session("Ship_View").ToString().Split(",")(0)
                txtDateTo.Text = Session("Ship_View").ToString().Split(",")(1)
                ' Session("Ship_View") = Nothing
            End If
        End If
    End Sub

    Private Sub btnSearch_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSearch.Click
        GenHTML_NewVersion()
    End Sub

    Private Sub GenHTML_NewVersion()
        Dim GCalendar As New CultureInfo("en-US")
        GCalendar.DateTimeFormat.Calendar = New GregorianCalendar()
        Thread.CurrentThread.CurrentCulture = GCalendar

        If ((txtDate.Text.Trim() = "") Or (txtDateTo.Text.Trim() = "")) Then
            ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", " alert('กรุณาทำการระบุช่วงการแสดงผล !!');", True)
            Exit Sub
        End If
        Try
            If (CDate(txtDate.Text) > CDate(txtDateTo.Text)) Then
                ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", " alert('ช่วงระยะเวลาการแสดงผลไม่ถูกต้อง!!');", True)
                Exit Sub
            End If
        Catch ex As Exception
            '  txtDate.Text = txtDate.Text.Split(" ")(0) & " " & txtDate.Text.Split(" ")(1) & " " & (CInt(txtDate.Text.Split(" ")(2)) )
            '  txtDateTo.Text = txtDateTo.Text.Split(" ")(0) & " " & txtDateTo.Text.Split(" ")(1) & " " & (CInt(txtDateTo.Text.Split(" ")(2)) )
        End Try

        Dim StartTime As String = ""
        Dim EndTime As String = ""
        StartTime = CDate(txtDate.Text).ToString("yyyyMMdd")  'วันเริ่มต้น
        EndTime = CDate(txtDateTo.Text).ToString("yyyyMMdd")  'วันสิ้นสุด

        Dim dsPORT As DataTable = clsGet.Get_WharfAll(ddlWharf.SelectedValue)
        Dim XHTML As String = ""
        Dim XHTML_Comment As String = ""
        Dim top As Integer = 54
        Dim Day As Integer = DateDiff(DateInterval.Day, CDate(txtDate.Text), CDate(txtDateTo.Text))

        Dim DayinMount As Integer = GetDayinMonth(CDate(txtDate.Text).Month.ToString("00"), CDate(txtDate.Text).Year)
        Dim DayDiff As Integer = 0
        If (CDate(txtDate.Text).Month.ToString("00") = CDate(txtDateTo.Text).Month.ToString("00")) Then
            DayDiff = CDate(txtDateTo.Text).Day - CDate(txtDate.Text).Day + 1
        Else
            DayDiff = DayinMount - CDate(txtDate.Text).Day + 1
        End If

        'If (Day >= DayDiff) Then
        'มีการข้ามเดือน
        Dim Month As Integer = 0
        XHTML = "<table width='" & (((Day + 1) * 140) + 80) & "px' style='z-index:100;position:relative' border='0' cellpadding='0' cellspacing='1'>"
        'Header
        XHTML &= "<tr >"
        XHTML &= "<td class='LockedColumnForTable2' align='center' rowspan='2' height='24px' style='width:80px;background-color:#ddffce;' ><b><font class='White-Style' style='color:#000000'>Wf.</font></b></td> "

        Dim chk As Boolean = True
        Dim COunt As Integer = 1
        Dim daycal As Integer = Day - DayDiff
        Dim chkManyMonth As Boolean = True
        If (daycal < 0) Then
            daycal = DayDiff - Day
            chkManyMonth = False
        End If
        While (chk)
            If (Month = 0) Then
                'If (DayinMount >= daycal) Then
                '    XHTML &= "<td align='center' colspan='" & DayDiff - 1 & "' height='24px' style='background-color:#ddffce;'  ><b><font class='White-Style' style='color:#000000'>&nbsp;" & CDate(txtDate.Text).ToString("MMMM", New CultureInfo("en-US")) & " " & CDate(txtDate.Text).ToString("yyyy") & "</font></b></td>"
                'Else
                XHTML &= "<td class='LockedColumnForTable2' align='center' colspan='" & DayDiff & "' height='24px' style='background-color:#ddffce;'  ><b><font class='White-Style' style='color:#000000'>&nbsp;" & CDate(txtDate.Text).ToString("MMMM", New CultureInfo("en-US")) & " " & CDate(txtDate.Text).ToString("yyyy") & "</font></b></td>"
                '   End If
            Else
                XHTML &= "<td class='LockedColumnForTable2' align='center' colspan='" & DayDiff & "' height='24px' style='background-color:#ddffce;'  ><b><font class='White-Style' style='color:#000000'>&nbsp;" & CDate(txtDate.Text).AddMonths(Month).ToString("MMMM", New CultureInfo("en-US")) & " " & CDate(txtDate.Text).AddMonths(Month).ToString("yyyy") & "</font></b></td>"
            End If

            DayinMount = GetDayinMonth(CDate(txtDate.Text).AddMonths(COunt).Month.ToString("00"), CDate(txtDate.Text).AddMonths(COunt).Year)
            Month += 1
            If (DayinMount >= daycal) Then
                'จบเดือน
                If (chkManyMonth) Then
                    XHTML &= "<td class='LockedColumnForTable2' align='center' colspan='" & daycal + 1 & "' height='24px' style='background-color:#ddffce;'  ><b><font class='White-Style' style='color:#000000'>&nbsp;" & CDate(txtDate.Text).AddMonths(Month).ToString("MMMM", New CultureInfo("en-US")) & " " & CDate(txtDate.Text).AddMonths(Month).ToString("yyyy") & "</font></b></td>"
                End If
                chk = False
            Else
                DayDiff = DayinMount
                daycal = daycal - DayDiff
            End If
            COunt += 1
        End While
        XHTML &= "</tr>"
        XHTML &= "<tr >"
        DayinMount = GetDayinMonth(CDate(txtDate.Text).Month.ToString("00"), CDate(txtDate.Text).Year)
        daycal = CDate(txtDate.Text).Day
        Month = 1

        Dim DateStart As Date = Convert.ToDateTime(txtDate.Text, GCalendar.DateTimeFormat)
        Dim DateStop As Date = Convert.ToDateTime(txtDateTo.Text, GCalendar.DateTimeFormat)
        Dim DayInDate As Date
        For i As Integer = 0 To Day
            If (daycal > DayinMount) Then
                daycal = 1
                DayinMount = GetDayinMonth(CDate(txtDate.Text).AddMonths(Month).Month.ToString("00"), CDate(txtDate.Text).AddMonths(Month).Year)
                Month += 1
            End If
            Dim txtday As String = ""
            If (Month = 1) Then
                'txtday = (CDate(CDate(txtDate.Text).Month.ToString("00") & " " & daycal & " " & CDate(txtDate.Text).Year).ToString("ddd", New CultureInfo("en-US")))
                DayInDate = GCalendar.Calendar.ToDateTime(DateStart.Year, DateStart.Month, daycal, 0, 0, 0, 0)
                txtday = DayInDate.ToString("ddd", GCalendar)
                If (clsGet.Get_Holiday(daycal.ToString("00"), DateStart.Month.ToString("00"), DateStart.Year)) Then
                    txtday = "Sat"
                End If
                If (Date.Now.ToString("dd MM yyyy", New CultureInfo("en-US")) = (daycal.ToString("00") & " " & DateStart.Month.ToString("00") & " " & DateStart.Year)) Then
                    txtday = "Current"
                End If
            Else
                DayInDate = GCalendar.Calendar.ToDateTime(DateStart.AddMonths(Month - 1).Year, DateStart.AddMonths(Month - 1).Month, daycal, 0, 0, 0, 0)
                txtday = DayInDate.ToString("ddd", GCalendar)
                If (clsGet.Get_Holiday(daycal.ToString("00"), DateStart.AddMonths(Month - 1).Month.ToString("00"), DateStart.AddMonths(Month - 1).Year)) Then
                    txtday = "Sat"
                End If
                If (Date.Now.ToString("dd MM yyyy", GCalendar) = (daycal.ToString("00") & " " & DateStart.AddMonths(Month - 1).Month.ToString("00") & " " & DateStart.AddMonths(Month - 1).Year)) Then
                    txtday = "Current"
                End If
            End If
            If (txtday = "Current") Then
                XHTML &= "<td class='LockedColumnForTable2' align='center' height='24px' style='width:140px;background-color:#5e9df4;' ><b><font class='White-Style' style='color:#000000'>&nbsp;" & daycal.ToString("00") & "</font></b></td>"
            ElseIf (txtday = "Sat") Or (txtday = "Sun") Then
                XHTML &= "<td class='LockedColumnForTable2' align='center' height='24px' style='width:140px;background-color:#f7f4a4;' ><b><font class='White-Style' style='color:#000000'>&nbsp;" & daycal.ToString("00") & "</font></b></td>"
            Else
                XHTML &= "<td class='LockedColumnForTable2' align='center' height='24px' style='width:140px;background-color:#ddffce;' ><b><font class='White-Style' style='color:#000000'>&nbsp;" & daycal.ToString("00") & "</font></b></td>"
            End If

            daycal += 1
        Next
        XHTML &= "</tr>"
        Dim row = 0
        For Each dr As DataRow In dsPORT.Rows
            Try
                Dim dsQueue As DataSet = clsGet.Get_Booking_WharfQueue(CInt(StartTime - 1), CInt(EndTime + 1), dr.Item("BookingWharf"))

                Dim dsView As DataView = dsQueue.Tables(0).DefaultView
                dsView.Sort = " D_FromSort "
                dsQueue = New DataSet
                dsQueue.Tables.Add(clsGet.ConvertDataViewToDataSet(dsView).Copy)

                Dim cQ As Integer = 1
                Dim D_From As String = ""
                Dim D_To As String = ""
                Dim T_From As String = ""
                Dim T_To As String = ""
                Dim dsQTemp As New DataSet
                dsQTemp = dsQueue.Copy
                Dim LoopFOr As Integer = dsQueue.Tables(0).Rows.Count - 1
                For x As Integer = 0 To LoopFOr
                    If (x > LoopFOr) Then
                        Exit For
                    End If
                    D_From = dsQueue.Tables(0).Rows((dsQueue.Tables(0).Rows.Count - 1) - x).Item("D_From") & ""
                    D_To = dsQueue.Tables(0).Rows((dsQueue.Tables(0).Rows.Count - 1) - x).Item("D_To") & ""
                    T_From = dsQueue.Tables(0).Rows((dsQueue.Tables(0).Rows.Count - 1) - x).Item("TimeStart") & ""
                    T_To = dsQueue.Tables(0).Rows((dsQueue.Tables(0).Rows.Count - 1) - x).Item("TimeEnd") & ""
                    GetBerth_Depart(dsQueue.Tables(0).Rows((dsQueue.Tables(0).Rows.Count - 1) - x).Item("Booking_no"), dsQueue.Tables(0).Rows((dsQueue.Tables(0).Rows.Count - 1) - x).Item("D_From") & " " & dsQueue.Tables(0).Rows((dsQueue.Tables(0).Rows.Count - 1) - x).Item("TimeStart"), dsQueue.Tables(0).Rows((dsQueue.Tables(0).Rows.Count - 1) - x).Item("D_To") & " " & dsQueue.Tables(0).Rows((dsQueue.Tables(0).Rows.Count - 1) - x).Item("TimeEnd"), D_From, D_To, T_From, T_To)
                    If (CDate(D_From) < CDate(txtDate.Text)) And (CDate(D_To) < CDate(txtDate.Text)) Then
                        dsQTemp.Tables(0).Rows((dsQueue.Tables(0).Rows.Count - 1) - x).Delete()
                        dsQTemp.AcceptChanges()
                    ElseIf (CDate(D_From) > CDate(txtDateTo.Text)) And (CDate(D_To) > CDate(txtDateTo.Text)) Then
                        dsQTemp.Tables(0).Rows((dsQueue.Tables(0).Rows.Count - 1) - x).Delete()
                        dsQTemp.AcceptChanges()
                    End If
                Next
                dsQueue = dsQTemp
                row += 1
                Dim bgcolorWharf As String = "#"
                If ((row Mod 2) = 1) Then
                    bgcolorWharf &= "ddffce"
                Else
                    bgcolorWharf &= "b1e899"
                End If
                If (dsQueue.Tables(0).Rows.Count > 0) Then
                    XHTML &= "<tr>"
                    XHTML &= "<td  class='LockedColumnForTable'  rowspan='" & dsQueue.Tables(0).Rows.Count * 2 & "' align='center' height='24px' style='width:80px;background-color:" & bgcolorWharf & ";' ><b><font class='White-Style' style='color:#000000'>" & dr.Item("Wharf") & "</font></b></td> "
                    For Each dq As DataRow In dsQueue.Tables(0).Rows
                        Dim dsBooking As DataSet = clsGet.Get_BookingOneRecord(dq.Item("Booking_no"))
                        Dim Booking As String = dq.Item("Booking_no")
                        D_From = dq.Item("D_From") & ""
                        D_To = dq.Item("D_To") & ""
                        T_From = dq.Item("TimeStart") & ""
                        T_To = dq.Item("TimeEnd") & ""
                        Dim color As String = "#FFFFFF"
                        If (dq.Item("Confirm_Time") & "" = "Y") Then
                            color = "../Image/Empty_Confirm.png"
                        Else
                            color = "#FFFFFF"
                        End If
                        Dim str As String = ""
                        Try
                            If (dq.Item("Note") & "" <> "") Then
                                str = "'"
                            End If
                        Catch ex As Exception

                        End Try

                        Dim Dateshow As String = dq.Item("D_From").ToString().Substring(0, 2) & "-" & dq.Item("D_To")
                        If (dsBooking.Tables(0).Rows.Count > 0) Then
                            Dateshow = dsBooking.Tables(0).Rows(0).Item("Date_From").ToString().Substring(0, 2) & "-" & dsBooking.Tables(0).Rows(0).Item("Date_To")
                            Dim Product_Event As String = clsGet.Get_ProductEvent(dq.Item("Booking_no"))
                            If (Product_Event = "L") Then
                                str &= "(L)"
                                If (dsBooking.Tables(0).Rows(0).Item("Vehicle_Destination") & "" = "O") Then
                                    'color = "#6698FF"
                                    If (dq.Item("Confirm_Time") & "" = "Y") Then
                                        color = "../Image/L_Foreign_confirm.png"
                                    Else
                                        color = "../Image/L_Foreign.png"
                                    End If
                                Else
                                    'color = "#A0CFEC"
                                    If (dq.Item("Confirm_Time") & "" = "Y") Then
                                        color = "../Image/L_Domestic_confirm.png"
                                    Else
                                        color = "../Image/L_Domestic.png"
                                    End If

                                End If
                            ElseIf (Product_Event = "D") Then
                                str &= "(D)"
                                If (dsBooking.Tables(0).Rows(0).Item("Vehicle_Destination") & "" = "O") Then
                                    'color = "#347C17"
                                    If (dq.Item("Confirm_Time") & "" = "Y") Then
                                        color = "../Image/D_Foreign_confirm.png"
                                    Else
                                        color = "../Image/D_Foreign.png"
                                    End If

                                Else
                                    'color = "#52D017"
                                    If (dq.Item("Confirm_Time") & "" = "Y") Then
                                        color = "../Image/D_Domestic_confirm.png"
                                    Else
                                        color = "../Image/D_Domestic.png"
                                    End If
                                End If
                            Else
                                If (dq.Item("Confirm_Time") & "" = "Y") Then
                                    color = "../Image/Empty_Confirm.png"
                                Else
                                    color = "#FFFFFF"
                                End If

                            End If

                            ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
                            '''''''''''''''''''''''''' Modifing ''''''''''''''''''''''''''''''''''''
                            ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
                            Dim dt As New DataTable()

                            Dim wharf As String = dr("BookingWharf").ToString()
                            Dim shiftingId As String = dq("Shifting_ID").ToString()
                            Dim bookingNo As String = dq("Booking_no").ToString()

                            Dim db As New DB_Class()
                            If (shiftingId = "") Then
                                db.queryString = "select bwp.Product_no,bp.Product_name,bwp.Product_volume as Product_Amount,bp.Product_Unit"
                                db.queryString &= ",bp.Product_Event,bp.Product_Buyer_name,bp.Product_Seller_name"
                                db.queryString &= " from BookingWharf_Product bwp "
                                ' db.queryString &= " left outer join Product p on bwp.Product_no=p.Product_no"
                                db.queryString &= " left outer join Booking_Product bp on (bp.Booking_no=bwp.Booking_no and bp.Product_no=bwp.Product_no)"
                                db.queryString &= " where bwp.Booking_no='" & bookingNo & "' and bwp.BookingWharf='" & wharf & "'"
                                Dim dtProductWharf As DataTable = db.getTable("productWharf")
                                If (dtProductWharf.Rows.Count > 0) Then
                                    dt = dtProductWharf
                                Else
                                    dt = clsGet.Get_Booking_Product(dq.Item("Booking_no"))
                                End If
                            Else
                                db.queryString = "select bsp.Product_no,bp.Product_name,bsp.Product_volume as Product_Amount,bp.Product_Unit"
                                db.queryString &= ",bp.Product_Event,bp.Product_Buyer_name,bp.Product_Seller_name"
                                db.queryString &= " from Booking_Shifting_Product bsp"
                                db.queryString &= " left outer join Booking_Product bp on (bp.Booking_no=bsp.Booking_no and bp.Product_no=bsp.Product_no)"
                                db.queryString &= " where bsp.Shifting_ID='" & shiftingId & "'"
                                Dim dtProductShifting As DataTable = db.getTable("productShifting")
                                If (dtProductShifting.Rows.Count > 0) Then
                                    dt = dtProductShifting
                                Else
                                    dt = clsGet.Get_Booking_Product(dq.Item("Booking_no"))
                                End If
                            End If
                          

                            ' dt = clsGet.Get_Booking_Product(dq.Item("Booking_no"))

                            Dim firstProduct As Boolean = True
                            Dim Owner_Name As String = ""
                            For Each drt As DataRow In dt.Rows
                                Dim Product_Amount As String = drt.Item("Product_Amount")
                                Dim Product_Unit As String = drt.Item("Product_Unit")
                                If (Product_Unit <> "KB") Then
                                    Dim dtamount As DataTable = clsGet.Get_Load_Unload_Product(dq.Item("Booking_no"), drt.Item("Product_no"))
                                    If (dtamount.Rows.Count > 0) Then
                                        If (ddlWharf.SelectedValue = "B") Then
                                            'Bulk
                                            Product_Amount = dtamount.Rows(0).Item("BU_Truck")
                                        Else
                                            'Liquid
                                            If (Product_Unit = "MT") Then
                                                Product_Amount = dtamount.Rows(0).Item("LU_BLMT")
                                            Else
                                                Product_Amount = dtamount.Rows(0).Item("LU_BL")
                                            End If
                                            If Product_Unit = "ML" Then
                                                Try
                                                    Product_Amount = (CDbl(Product_Amount * 1000) / 1000000).ToString("#######################0.000")
                                                Catch ex As Exception
                                                    'Product_Amount = "0"
                                                End Try

                                            End If
                                        End If
                                    End If


                                End If
                                If (firstProduct) Then
                                    str &= drt.Item("Product_Name") & " ," & Product_Amount & " " & Product_Unit
                                    firstProduct = False
                                Else
                                    str &= "/" & drt.Item("Product_Name") & " ," & drt.Item("Product_Amount") & " " & drt.Item("Product_Unit")
                                End If
                                If (dsBooking.Tables(0).Rows(0).Item("PORT") & "" = "L") Then
                                    'Liquid
                                    If (drt.Item("Product_Event") & "" = "L") Then
                                        If (Owner_Name = "") Then
                                            Owner_Name = drt.Item("Product_Buyer_name") & ""
                                        Else
                                            Owner_Name &= "," & drt.Item("Product_Buyer_name") & ""
                                        End If
                                    Else
                                        If (Owner_Name = "") Then
                                            Owner_Name = drt.Item("Product_Seller_name") & ""
                                        Else
                                            Owner_Name &= "," & drt.Item("Product_Seller_name") & ""
                                        End If
                                    End If
                                Else
                                    'Bulk
                                    If (Owner_Name = "") Then
                                        Owner_Name = drt.Item("Owner_Product_name") & ""
                                    Else
                                        Owner_Name &= "," & drt.Item("Owner_Product_name") & ""
                                    End If
                                End If
                            Next
                            str &= "(" & dsBooking.Tables(0).Rows(0).Item("Vehicle_Text") & ")"
                            If (dsBooking.Tables(0).Rows(0).Item("BunkerFO_Point") & "" <> "0") Or (dsBooking.Tables(0).Rows(0).Item("BunkerDO_Point") & "" <> "0") Then
                                str &= "(Bunker FO " & dsBooking.Tables(0).Rows(0).Item("BunkerFO_Point") & "" & " DO " & dsBooking.Tables(0).Rows(0).Item("BunkerDO_Point") & " )"
                            End If
                            'เพิ่ม เวลา เพิ่ม Agent Remark
                            GetBerth_Depart(dq.Item("Booking_no"), dq.Item("D_From") & " " & dq.Item("TimeStart"), dq.Item("D_To") & " " & dq.Item("TimeEnd"), D_From, D_To, T_From, T_To)
                            str &= Owner_Name
                        End If



                        If (dq.Item("Wharf_Remark") & "" <> "") Then
                            str &= "(" & dq.Item("Wharf_Remark") & ")"
                        End If
                        If (dsBooking.Tables(0).Rows.Count > 0) Then
                            If (dsBooking.Tables(0).Rows(0).Item("Lay_Time") & "" <> "0") Then
                                str &= "(LAYTIME " & dsBooking.Tables(0).Rows(0).Item("Lay_Time") & " Hrs.)"
                            End If
                        End If
                        Dim bgcolor As String = "#"
                        If ((row Mod 2) = 1) Then
                            bgcolor &= "edeeed"
                        Else
                            bgcolor &= "cbcdcb"
                        End If

                        Dim left As Integer = 85
                        If (cQ Mod 2 = 1) Then
                            'ไว้สำหรับแสดง Comment
                            For i As Integer = 0 To Day
                                XHTML &= "<td align='center' height='24px' style='width:140px;background-color:" & bgcolor & ";' >&nbsp;</td>"
                            Next
                            XHTML &= "</tr>"
                            XHTML &= "<tr>"
                        End If
                        Dim tDate As Integer = 0
                        Dim tDateto As Integer = 0
                        Dim tDate2 As Integer = 0
                        Dim tDate2to As Integer = 0
                        Try
                            tDate = CInt(txtDate.Text.Split(" ")(2))
                        Catch ex As Exception
                            tDate = CInt(txtDate.Text.Substring(txtDate.Text.Length - 5, 4))
                        End Try
                        Try
                            tDateto = CInt(txtDateTo.Text.Split(" ")(2))
                        Catch ex As Exception
                            tDateto = CInt(txtDateTo.Text.Substring(txtDateTo.Text.Length - 5, 4))
                        End Try
                        Try
                            tDate2 = CInt(D_From.Split(" ")(2))
                        Catch ex As Exception
                            tDate2 = CInt(D_From.Substring(D_From.Length - 5, 4))
                        End Try
                        Try
                            tDate2to = CInt(D_To.Split(" ")(2))
                        Catch ex As Exception
                            tDate2to = CInt(D_To.Substring(D_To.Length - 5, 4))
                        End Try
                        Dim ColumnEmpty As Integer = DateDiff(DateInterval.Day, CDate((txtDate.Text.Substring(0, txtDate.Text.Length - 5)) & tDate), CDate((D_From.ToString().Substring(0, D_From.Length - 5)) & tDate2))
                        For a As Integer = 0 To ColumnEmpty - 1
                            left += 140
                            XHTML &= "<td align='center' height='24px' style='width:140px;background-color:" & bgcolor & ";' >&nbsp;</td>"
                        Next
                        Dim colspanColumn As Integer
                        If (CDate(D_From) > CDate(txtDate.Text)) Then
                            colspanColumn = DateDiff(DateInterval.Day, CDate((D_From.Substring(0, D_From.Length - 5)) & tDate), CDate((D_To.ToString().Substring(0, D_To.Length - 5)) & tDate2to))
                        Else
                            If (D_To > CDate(txtDateTo.Text)) Then
                                colspanColumn = DateDiff(DateInterval.Day, CDate((txtDate.Text.Substring(0, txtDate.Text.Length - 5)) & tDate), CDate((txtDateTo.Text.Substring(0, txtDateTo.Text.Length - 5)) & tDateto)) + 1
                            Else
                                colspanColumn = DateDiff(DateInterval.Day, CDate((txtDate.Text.Substring(0, txtDate.Text.Length - 5)) & tDate), CDate((D_To.ToString().Substring(0, D_To.Length - 5)) & tDate2to))
                            End If

                        End If
                        Dim _EndTime As String = T_To
                        Try
                            If (CDate(D_To) > CDate(txtDateTo.Text)) Then
                                _EndTime = ""
                                T_To = "23:59"
                            End If
                        Catch ex As Exception

                        End Try

                        If (colspanColumn + ColumnEmpty) >= Day Then
                            XHTML &= GenColorTab(dq.Item("Confirm_Time"), (Day - ColumnEmpty) + 1, T_From.Split(":")(0), T_To.Split(":")(0), T_From.Split(":")(1), T_To.Split(":")(1), color, dsBooking, Dateshow, T_From, _EndTime, D_From, D_To, row, Booking)
                        Else

                            XHTML &= GenColorTab(dq.Item("Confirm_Time"), colspanColumn + 1, T_From.Split(":")(0), T_To.Split(":")(0), T_From.Split(":")(1), T_To.Split(":")(1), color, dsBooking, Dateshow, T_From, _EndTime, D_From, D_To, row, Booking)

                            If (CDate(D_From) < CDate(txtDate.Text)) Then
                                Dim Sub_Day As Integer = DateDiff(DateInterval.Day, CDate(D_From), CDate(txtDate.Text))
                                For a As Integer = 1 To (Day - (colspanColumn + ColumnEmpty)) - Sub_Day
                                    XHTML &= "<td align='center' height='24px' style='width:140px;background-color:" & bgcolor & ";' >&nbsp;</td>"
                                Next
                            Else
                                Dim Loop_For As Integer = 0
                                If (colspanColumn + ColumnEmpty) < 0 Then
                                    Loop_For = Day
                                Else
                                    Loop_For = (Day - (colspanColumn + ColumnEmpty))
                                End If
                                For a As Integer = 1 To Loop_For
                                    XHTML &= "<td align='center' height='24px' style='width:140px;background-color:" & bgcolor & ";' >&nbsp;</td>"
                                Next
                            End If
                        End If
                        '************************************
                        '**********************
                        '***************************
                        '**************************************

                        XHTML_Comment &= GenComment(left.ToString, (top).ToString, str)

                        top += 50
                        XHTML &= "</tr>"
                        cQ += 2
                    Next
                Else
                    XHTML &= "<tr>"
                    XHTML &= "<td align='center'  class='LockedColumnForTable' height='24px' style='width:80px;background-color:" & bgcolorWharf & ";' ><b><font class='White-Style' style='color:#000000'>" & dr.Item("Wharf") & "</font></b></td> "
                    Dim bgcolor As String = "#"
                    If ((row Mod 2) = 1) Then
                        bgcolor &= "edeeed"
                    Else
                        bgcolor &= "cbcdcb"
                    End If

                    For i As Integer = 0 To Day
                        XHTML &= "<td align='center' height='24px' style='width:140px;background-color:" & bgcolor & ";' >&nbsp;</td>"
                    Next
                    top += 25
                End If


                XHTML &= "</tr>"
            Catch ex As Exception

            End Try

        Next

        XHTML &= "</table>"


        XHTML &= XHTML_Comment
        LiWharf.Text = XHTML
        Session("Ship_View") = txtDate.Text & "," & txtDateTo.Text
    End Sub

    Private Function GenColorTab(ByVal Confirm_Time As String, ByVal colspan As Integer, ByVal HourFrom As String, ByVal HourTo As String, ByVal MinuteForm As String, ByVal MinuteTo As String, ByVal color As String, ByVal dsBooking As DataSet, ByVal Dateshow As String, ByVal StartTime As String, ByVal EndTime As String, ByVal D_S As String, ByVal D_E As String, ByVal row As Integer, ByVal Booking As String) As String
        Dim HTML As String = ""
        Dim bgcolor As String = "#"
        If ((row Mod 2) = 1) Then
            bgcolor &= "edeeed"
        Else
            bgcolor &= "cbcdcb"
        End If
        If (colspan = 1) Then
            If (dsBooking.Tables(0).Rows.Count > 0) Then
                HTML = "<td align='center' width='140px' height='24px' onmouseover=showtooltip('tooltip1','" & dsBooking.Tables(0).Rows(0).Item("Booking_no").ToString().Replace(" ", "\xa0") & "','" & dsBooking.Tables(0).Rows(0).Item("MAgent_name").ToString().Replace(" ", "\xa0") & "','" & dsBooking.Tables(0).Rows(0).Item("Vehicle_Text").ToString().Replace(" ", "\xa0") & "','" & dsBooking.Tables(0).Rows(0).Item("Vehicle_LOA").ToString().Replace(" ", "\xa0") & "','" & dsBooking.Tables(0).Rows(0).Item("Vehicle_DWT").ToString().Replace(" ", "\xa0") & "','" & dsBooking.Tables(0).Rows(0).Item("Vehicle_Draft").ToString().Replace(" ", "\xa0") & "','" & dsBooking.Tables(0).Rows(0).Item("Vehicle_GRT").ToString().Replace(" ", "\xa0") & "','" & Dateshow.Replace(" ", "\xa0") & "','" & dsBooking.Tables(0).Rows(0).Item("Product_name").ToString().Replace(" ", "\xa0") & "','" & dsBooking.Tables(0).Rows(0).Item("Product_Amount").ToString().Replace(" ", "\xa0") & "','" & dsBooking.Tables(0).Rows(0).Item("Lay_Time").ToString().Replace(" ", "\xa0") & "'); onmouseout=hidetooltip('tooltip1'); onclick=fncdbl('" & dsBooking.Tables(0).Rows(0).Item("Booking_no") & "'); >"
            Else
                HTML = "<td align='center' width='140px' height='24px' onclick=fncdbl2('" & Booking & "'); >"
            End If

            HTML &= "<table width='100%' border='0' cellpadding='0' cellspacing='0'><tr>"
            If (CDate(D_S) < CDate(txtDate.Text)) Then
                If (HourTo < 7) Then
                    '0-6
                    If (HourTo = 6) Then
                        If (MinuteTo < 1) Then
                            HTML &= GenTdFourOpen(Confirm_Time, color, bgcolor, bgcolor, bgcolor, colspan, StartTime, EndTime, D_S, D_E)
                        Else
                            HTML &= GenTdFourOpen(Confirm_Time, color, color, bgcolor, bgcolor, colspan, StartTime, EndTime, D_S, D_E)
                        End If
                    Else
                        HTML &= GenTdFourOpen(Confirm_Time, color, bgcolor, bgcolor, bgcolor, colspan, StartTime, EndTime, D_S, D_E)
                    End If


                ElseIf (HourTo < 13) Then
                    '7-12
                    If HourTo = 12 Then
                        If (MinuteTo < 1) Then
                            HTML &= GenTdFourOpen(Confirm_Time, color, color, bgcolor, bgcolor, colspan, StartTime, EndTime, D_S, D_E)
                        Else
                            HTML &= GenTdFourOpen(Confirm_Time, color, color, color, bgcolor, colspan, StartTime, EndTime, D_S, D_E)
                        End If
                    Else
                        HTML &= GenTdFourOpen(Confirm_Time, bgcolor, color, bgcolor, bgcolor, colspan, StartTime, EndTime, D_S, D_E)
                    End If


                ElseIf (HourTo < 19) Then
                    '13-18
                    If (HourTo = 18) Then
                        If (MinuteTo < 1) Then
                            HTML &= GenTdFourOpen(Confirm_Time, color, color, color, bgcolor, colspan, StartTime, EndTime, D_S, D_E)
                        Else
                            HTML &= GenTdFourOpen(Confirm_Time, color, color, color, color, colspan, StartTime, EndTime, D_S, D_E)
                        End If
                    Else
                        HTML &= GenTdFourOpen(Confirm_Time, color, color, color, bgcolor, colspan, StartTime, EndTime, D_S, D_E)
                    End If


                Else
                    '19-24
                    HTML &= GenTdFourOpen(Confirm_Time, color, color, color, color, colspan, StartTime, EndTime, D_S, D_E)
                End If
            ElseIf (HourFrom < 6) Then
                '0-6
                If (HourTo < 7) Then
                    '0-6
                    If (HourTo = 6) Then
                        If (MinuteTo < 1) Then
                            HTML &= GenTdFourOpen(Confirm_Time, color, bgcolor, bgcolor, bgcolor, colspan, StartTime, EndTime, D_S, D_E)
                        Else
                            HTML &= GenTdFourOpen(Confirm_Time, color, color, bgcolor, bgcolor, colspan, StartTime, EndTime, D_S, D_E)
                        End If
                    Else
                        HTML &= GenTdFourOpen(Confirm_Time, color, bgcolor, bgcolor, bgcolor, colspan, StartTime, EndTime, D_S, D_E)
                    End If


                ElseIf (HourTo < 13) Then
                    '7-12
                    If HourTo = 12 Then
                        If (MinuteTo < 1) Then
                            HTML &= GenTdFourOpen(Confirm_Time, color, color, bgcolor, bgcolor, colspan, StartTime, EndTime, D_S, D_E)
                        Else
                            HTML &= GenTdFourOpen(Confirm_Time, color, color, color, bgcolor, colspan, StartTime, EndTime, D_S, D_E)
                        End If
                    Else
                        HTML &= GenTdFourOpen(Confirm_Time, color, color, bgcolor, bgcolor, colspan, StartTime, EndTime, D_S, D_E)
                    End If


                ElseIf (HourTo < 19) Then
                    '13-18
                    If HourTo = 18 Then
                        If (MinuteTo < 1) Then
                            HTML &= GenTdFourOpen(Confirm_Time, color, color, color, bgcolor, colspan, StartTime, EndTime, D_S, D_E)
                        Else
                            HTML &= GenTdFourOpen(Confirm_Time, color, color, color, color, colspan, StartTime, EndTime, D_S, D_E)
                        End If
                    Else
                        HTML &= GenTdFourOpen(Confirm_Time, color, color, color, bgcolor, colspan, StartTime, EndTime, D_S, D_E)
                    End If

                Else
                    '19-24
                    HTML &= GenTdFourOpen(Confirm_Time, color, color, color, color, colspan, StartTime, EndTime, D_S, D_E)
                End If
            ElseIf (HourFrom < 12) Then
                '7-12
                If (HourTo < 13) Then
                    '7-12
                    If HourTo = 12 Then
                        If (MinuteTo < 1) Then
                            HTML &= GenTdFourOpen(Confirm_Time, bgcolor, color, bgcolor, bgcolor, colspan, StartTime, EndTime, D_S, D_E)
                        Else
                            HTML &= GenTdFourOpen(Confirm_Time, bgcolor, color, color, bgcolor, colspan, StartTime, EndTime, D_S, D_E)
                        End If
                    Else
                        HTML &= GenTdFourOpen(Confirm_Time, bgcolor, color, bgcolor, bgcolor, colspan, StartTime, EndTime, D_S, D_E)
                    End If


                ElseIf (HourTo < 19) Then
                    '13-18
                    If (HourTo = 18) Then
                        If (MinuteTo < 1) Then
                            HTML &= GenTdFourOpen(Confirm_Time, bgcolor, color, color, bgcolor, colspan, StartTime, EndTime, D_S, D_E)
                        Else
                            HTML &= GenTdFourOpen(Confirm_Time, bgcolor, color, color, color, colspan, StartTime, EndTime, D_S, D_E)
                        End If
                    Else
                        HTML &= GenTdFourOpen(Confirm_Time, bgcolor, color, color, bgcolor, colspan, StartTime, EndTime, D_S, D_E)
                    End If


                Else
                    '19-24
                    HTML &= GenTdFourOpen(Confirm_Time, bgcolor, color, color, color, colspan, StartTime, EndTime, D_S, D_E)
                End If
            ElseIf (HourFrom < 18) Then
                '13-18
                If (HourTo < 19) Then
                    '13-18
                    If HourTo = 18 Then
                        If (MinuteTo < 1) Then
                            HTML &= GenTdFourOpen(Confirm_Time, bgcolor, bgcolor, color, bgcolor, colspan, StartTime, EndTime, D_S, D_E)
                        Else
                            HTML &= GenTdFourOpen(Confirm_Time, bgcolor, bgcolor, color, color, colspan, StartTime, EndTime, D_S, D_E)
                        End If
                    Else
                        HTML &= GenTdFourOpen(Confirm_Time, bgcolor, bgcolor, color, bgcolor, colspan, StartTime, EndTime, D_S, D_E)
                    End If


                Else
                    '19-24
                    HTML &= GenTdFourOpen(Confirm_Time, bgcolor, bgcolor, color, color, colspan, StartTime, EndTime, D_S, D_E)
                End If
            Else
                '19-24
                HTML &= GenTdFourOpen(Confirm_Time, bgcolor, bgcolor, bgcolor, color, colspan, StartTime, EndTime, D_S, D_E)
            End If
            HTML &= "</tr></table>"
            HTML &= "</td>"
        ElseIf (colspan = 2) Then
            If (dsBooking.Tables(0).Rows.Count > 0) Then
                HTML = "<td align='center'  width='140px' height='24px' onmouseover=showtooltip('tooltip1','" & dsBooking.Tables(0).Rows(0).Item("Booking_no").ToString().Replace(" ", "\xa0") & "','" & dsBooking.Tables(0).Rows(0).Item("MAgent_name").ToString().Replace(" ", "\xa0") & "','" & dsBooking.Tables(0).Rows(0).Item("Vehicle_Text").ToString().Replace(" ", "\xa0") & "','" & dsBooking.Tables(0).Rows(0).Item("Vehicle_LOA").ToString().Replace(" ", "\xa0") & "','" & dsBooking.Tables(0).Rows(0).Item("Vehicle_DWT").ToString().Replace(" ", "\xa0") & "','" & dsBooking.Tables(0).Rows(0).Item("Vehicle_Draft").ToString().Replace(" ", "\xa0") & "','" & dsBooking.Tables(0).Rows(0).Item("Vehicle_GRT").ToString().Replace(" ", "\xa0") & "','" & Dateshow.Replace(" ", "\xa0") & "','" & dsBooking.Tables(0).Rows(0).Item("Product_name").ToString().Replace(" ", "\xa0") & "','" & dsBooking.Tables(0).Rows(0).Item("Product_Amount").ToString().Replace(" ", "\xa0") & "','" & dsBooking.Tables(0).Rows(0).Item("Lay_Time").ToString().Replace(" ", "\xa0") & "'); onmouseout=hidetooltip('tooltip1'); onclick=fncdbl('" & dsBooking.Tables(0).Rows(0).Item("Booking_no") & "'); >"
            Else
                HTML = "<td align='center'  width='140px' height='24px'  onclick=fncdbl2('" & Booking & "'); >"
            End If

            HTML &= "<table width='100%' border='0' cellpadding='0' cellspacing='0'><tr>"
            If (CDate(D_S) < CDate(txtDate.Text)) Then
                HTML &= GenTdFourOpen(Confirm_Time, color, color, color, color, colspan, StartTime, EndTime, D_S, D_E)
            ElseIf (HourFrom < 6) Then
                HTML &= GenTdFourOpen(Confirm_Time, color, color, color, color, colspan, StartTime, EndTime, D_S, D_E)
            ElseIf (HourFrom < 12) Then
                HTML &= GenTdFourOpen(Confirm_Time, bgcolor, color, color, color, colspan, StartTime, EndTime, D_S, D_E)
            ElseIf (HourFrom < 18) Then
                HTML &= GenTdFourOpen(Confirm_Time, bgcolor, bgcolor, color, color, colspan, StartTime, EndTime, D_S, D_E)
            Else
                '19-24
                HTML &= GenTdFourOpen(Confirm_Time, bgcolor, bgcolor, bgcolor, color, colspan, StartTime, EndTime, D_S, D_E)
            End If
            HTML &= "</tr></table>"
            HTML &= "</td>"

            If (dsBooking.Tables(0).Rows.Count > 0) Then
                HTML &= "<td align='center'  width='140px' height='24px' onmouseover=showtooltip('tooltip1','" & dsBooking.Tables(0).Rows(0).Item("Booking_no").ToString().Replace(" ", "\xa0") & "','" & dsBooking.Tables(0).Rows(0).Item("MAgent_name").ToString().Replace(" ", "\xa0") & "','" & dsBooking.Tables(0).Rows(0).Item("Vehicle_Text").ToString().Replace(" ", "\xa0") & "','" & dsBooking.Tables(0).Rows(0).Item("Vehicle_LOA").ToString().Replace(" ", "\xa0") & "','" & dsBooking.Tables(0).Rows(0).Item("Vehicle_DWT").ToString().Replace(" ", "\xa0") & "','" & dsBooking.Tables(0).Rows(0).Item("Vehicle_Draft").ToString().Replace(" ", "\xa0") & "','" & dsBooking.Tables(0).Rows(0).Item("Vehicle_GRT").ToString().Replace(" ", "\xa0") & "','" & Dateshow.Replace(" ", "\xa0") & "','" & dsBooking.Tables(0).Rows(0).Item("Product_name").ToString().Replace(" ", "\xa0") & "','" & dsBooking.Tables(0).Rows(0).Item("Product_Amount").ToString().Replace(" ", "\xa0") & "','" & dsBooking.Tables(0).Rows(0).Item("Lay_Time").ToString().Replace(" ", "\xa0") & "'); onmouseout=hidetooltip('tooltip1'); onclick=fncdbl('" & dsBooking.Tables(0).Rows(0).Item("Booking_no") & "'); >"
            Else
                HTML &= "<td align='center'  width='140px' height='24px'  onclick=fncdbl2('" & Booking & "'); >"
            End If

            HTML &= "<table width='100%' border='0' cellpadding='0' cellspacing='0'><tr>"
            If (HourTo < 7) Then
                '0-6
                If HourTo = 6 Then
                    If (MinuteTo < 1) Then
                        HTML &= GenTdFourClose(Confirm_Time, color, bgcolor, bgcolor, bgcolor, colspan, EndTime)
                    Else
                        HTML &= GenTdFourClose(Confirm_Time, color, color, bgcolor, bgcolor, colspan, EndTime)
                    End If
                Else
                    HTML &= GenTdFourClose(Confirm_Time, color, bgcolor, bgcolor, bgcolor, colspan, EndTime)
                End If


            ElseIf (HourTo < 13) Then
                '7-12
                If HourTo = 12 Then
                    If (MinuteTo < 1) Then
                        HTML &= GenTdFourClose(Confirm_Time, color, color, bgcolor, bgcolor, colspan, EndTime)
                    Else
                        HTML &= GenTdFourClose(Confirm_Time, color, color, color, bgcolor, colspan, EndTime)
                    End If
                Else
                    HTML &= GenTdFourClose(Confirm_Time, color, color, bgcolor, bgcolor, colspan, EndTime)
                End If


            ElseIf (HourTo < 19) Then
                '13-18
                If HourTo = 18 Then
                    If (MinuteTo < 1) Then
                        HTML &= GenTdFourClose(Confirm_Time, color, color, color, bgcolor, colspan, EndTime)
                    Else
                        HTML &= GenTdFourClose(Confirm_Time, color, color, color, color, colspan, EndTime)
                    End If
                Else
                    HTML &= GenTdFourClose(Confirm_Time, color, color, color, bgcolor, colspan, EndTime)
                End If

            Else
                '19-24
                HTML &= GenTdFourClose(Confirm_Time, color, color, color, color, colspan, EndTime)
            End If
            HTML &= "</tr></table>"
            HTML &= "</td>"
        Else
            If (dsBooking.Tables(0).Rows.Count > 0) Then
                HTML = "<td align='center'  width='140px' height='24px' onmouseover=showtooltip('tooltip1','" & dsBooking.Tables(0).Rows(0).Item("Booking_no").ToString().Replace(" ", "\xa0") & "','" & dsBooking.Tables(0).Rows(0).Item("MAgent_name").ToString().Replace(" ", "\xa0") & "','" & dsBooking.Tables(0).Rows(0).Item("Vehicle_Text").ToString().Replace(" ", "\xa0") & "','" & dsBooking.Tables(0).Rows(0).Item("Vehicle_LOA").ToString().Replace(" ", "\xa0") & "','" & dsBooking.Tables(0).Rows(0).Item("Vehicle_DWT").ToString().Replace(" ", "\xa0") & "','" & dsBooking.Tables(0).Rows(0).Item("Vehicle_Draft").ToString().Replace(" ", "\xa0") & "','" & dsBooking.Tables(0).Rows(0).Item("Vehicle_GRT").ToString().Replace(" ", "\xa0") & "','" & Dateshow.Replace(" ", "\xa0") & "','" & dsBooking.Tables(0).Rows(0).Item("Product_name").ToString().Replace(" ", "\xa0") & "','" & dsBooking.Tables(0).Rows(0).Item("Product_Amount").ToString().Replace(" ", "\xa0") & "','" & dsBooking.Tables(0).Rows(0).Item("Lay_Time").ToString().Replace(" ", "\xa0") & "'); onmouseout=hidetooltip('tooltip1'); onclick=fncdbl('" & dsBooking.Tables(0).Rows(0).Item("Booking_no") & "'); >"
            Else
                HTML = "<td align='center'  width='140px' height='24px'  onclick=fncdbl2('" & Booking & "'); >"
            End If

            HTML &= "<table width='100%' border='0' cellpadding='0' cellspacing='0'><tr>"
            If (CDate(D_S) < CDate(txtDate.Text)) Then
                HTML &= GenTdFourOpen(Confirm_Time, color, color, color, color, colspan, StartTime, EndTime, D_S, D_E)
            ElseIf (HourFrom < 6) Then
                HTML &= GenTdFourOpen(Confirm_Time, color, color, color, color, colspan, StartTime, EndTime, D_S, D_E)
            ElseIf (HourFrom < 12) Then
                HTML &= GenTdFourOpen(Confirm_Time, bgcolor, color, color, color, colspan, StartTime, EndTime, D_S, D_E)
            ElseIf (HourFrom < 18) Then
                HTML &= GenTdFourOpen(Confirm_Time, bgcolor, bgcolor, color, color, colspan, StartTime, EndTime, D_S, D_E)
            Else
                '19-24
                HTML &= GenTdFourOpen(Confirm_Time, bgcolor, bgcolor, bgcolor, color, colspan, StartTime, EndTime, D_S, D_E)
            End If
            HTML &= "</tr></table>"
            HTML &= "</td>"

            If (dsBooking.Tables(0).Rows.Count > 0) Then
                Dim chk As Boolean = True
                If (CDate(D_S) < CDate(txtDate.Text)) Then
                    If (CDate(D_E) > CDate(txtDateTo.Text)) Then
                        chk = False
                        HTML &= "<td colspan='" & colspan - 3 & "' align='center' height='24px' style='width:140px;background-image:url(" & color & ");' onmouseover=showtooltip('tooltip1','" & dsBooking.Tables(0).Rows(0).Item("Booking_no").ToString().Replace(" ", "\xa0") & "','" & dsBooking.Tables(0).Rows(0).Item("MAgent_name").ToString().Replace(" ", "\xa0") & "','" & dsBooking.Tables(0).Rows(0).Item("Vehicle_Text").ToString().Replace(" ", "\xa0") & "','" & dsBooking.Tables(0).Rows(0).Item("Vehicle_LOA").ToString().Replace(" ", "\xa0") & "','" & dsBooking.Tables(0).Rows(0).Item("Vehicle_DWT").ToString().Replace(" ", "\xa0") & "','" & dsBooking.Tables(0).Rows(0).Item("Vehicle_Draft").ToString().Replace(" ", "\xa0") & "','" & dsBooking.Tables(0).Rows(0).Item("Vehicle_GRT").ToString().Replace(" ", "\xa0") & "','" & Dateshow.Replace(" ", "\xa0") & "','" & dsBooking.Tables(0).Rows(0).Item("Product_name").ToString().Replace(" ", "\xa0") & "','" & dsBooking.Tables(0).Rows(0).Item("Product_Amount").ToString().Replace(" ", "\xa0") & "','" & dsBooking.Tables(0).Rows(0).Item("Lay_Time").ToString().Replace(" ", "\xa0") & "'); onmouseout=hidetooltip('tooltip1'); onclick=fncdbl('" & dsBooking.Tables(0).Rows(0).Item("Booking_no") & "'); >&nbsp;</td>"
                    End If
                End If
                If (chk) Then
                    HTML &= "<td colspan='" & colspan - 2 & "' align='center' height='24px' style='width:140px;background-image:url(" & color & ");' onmouseover=showtooltip('tooltip1','" & dsBooking.Tables(0).Rows(0).Item("Booking_no").ToString().Replace(" ", "\xa0") & "','" & dsBooking.Tables(0).Rows(0).Item("MAgent_name").ToString().Replace(" ", "\xa0") & "','" & dsBooking.Tables(0).Rows(0).Item("Vehicle_Text").ToString().Replace(" ", "\xa0") & "','" & dsBooking.Tables(0).Rows(0).Item("Vehicle_LOA").ToString().Replace(" ", "\xa0") & "','" & dsBooking.Tables(0).Rows(0).Item("Vehicle_DWT").ToString().Replace(" ", "\xa0") & "','" & dsBooking.Tables(0).Rows(0).Item("Vehicle_Draft").ToString().Replace(" ", "\xa0") & "','" & dsBooking.Tables(0).Rows(0).Item("Vehicle_GRT").ToString().Replace(" ", "\xa0") & "','" & Dateshow.Replace(" ", "\xa0") & "','" & dsBooking.Tables(0).Rows(0).Item("Product_name").ToString().Replace(" ", "\xa0") & "','" & dsBooking.Tables(0).Rows(0).Item("Product_Amount").ToString().Replace(" ", "\xa0") & "','" & dsBooking.Tables(0).Rows(0).Item("Lay_Time").ToString().Replace(" ", "\xa0") & "'); onmouseout=hidetooltip('tooltip1'); onclick=fncdbl('" & dsBooking.Tables(0).Rows(0).Item("Booking_no") & "'); >&nbsp;</td>"
                End If


                HTML &= "<td align='center'  width='140px' height='24px' onmouseover=showtooltip('tooltip1','" & dsBooking.Tables(0).Rows(0).Item("Booking_no").ToString().Replace(" ", "\xa0") & "','" & dsBooking.Tables(0).Rows(0).Item("MAgent_name").ToString().Replace(" ", "\xa0") & "','" & dsBooking.Tables(0).Rows(0).Item("Vehicle_Text").ToString().Replace(" ", "\xa0") & "','" & dsBooking.Tables(0).Rows(0).Item("Vehicle_LOA").ToString().Replace(" ", "\xa0") & "','" & dsBooking.Tables(0).Rows(0).Item("Vehicle_DWT").ToString().Replace(" ", "\xa0") & "','" & dsBooking.Tables(0).Rows(0).Item("Vehicle_Draft").ToString().Replace(" ", "\xa0") & "','" & dsBooking.Tables(0).Rows(0).Item("Vehicle_GRT").ToString().Replace(" ", "\xa0") & "','" & Dateshow.Replace(" ", "\xa0") & "','" & dsBooking.Tables(0).Rows(0).Item("Product_name").ToString().Replace(" ", "\xa0") & "','" & dsBooking.Tables(0).Rows(0).Item("Product_Amount").ToString().Replace(" ", "\xa0") & "','" & dsBooking.Tables(0).Rows(0).Item("Lay_Time").ToString().Replace(" ", "\xa0") & "'); onmouseout=hidetooltip('tooltip1'); onclick=fncdbl('" & dsBooking.Tables(0).Rows(0).Item("Booking_no") & "'); >"
            Else
                Dim chk As Boolean = True
                If (CDate(D_S) < CDate(txtDate.Text)) Then
                    If (CDate(D_E) > CDate(txtDateTo.Text)) Then
                        chk = False
                        Dim Day As Integer = DateDiff(DateInterval.Day, CDate(txtDate.Text), CDate(txtDateTo.Text))
                        If ((colspan - 3) > Day - 1) Then
                            HTML &= "<td colspan='" & Day - 1 & "' align='center' height='24px' style='width:140px;background-image:url(" & color & ");'  onclick=fncdbl2('" & Booking & "'); >&nbsp;</td>"
                        Else
                            HTML &= "<td colspan='" & colspan - 3 & "' align='center' height='24px' style='width:140px;background-image:url(" & color & ");'  onclick=fncdbl2('" & Booking & "'); >&nbsp;</td>"
                        End If

                    End If
                End If

                If (chk) Then
                    HTML &= "<td colspan='" & colspan - 2 & "' align='center' height='24px' style='width:140px;background-image:url(" & color & ");'  onclick=fncdbl2('" & Booking & "'); >&nbsp;</td>"
                End If

                HTML &= "<td align='center'  width='140px' height='24px'  onclick=fncdbl2('" & Booking & "'); >"
            End If
            HTML &= "<table width='100%' border='0' cellpadding='0' cellspacing='0'><tr>"
            If (CDate(D_E) > CDate(txtDateTo.Text)) Then
                HTML &= GenTdFourClose(Confirm_Time, color, color, color, color, colspan, "")
            ElseIf (HourTo < 7) Then
                '0-6
                If HourTo = 6 Then
                    If (MinuteTo < 1) Then
                        HTML &= GenTdFourClose(Confirm_Time, color, bgcolor, bgcolor, bgcolor, colspan, EndTime)
                    Else
                        HTML &= GenTdFourClose(Confirm_Time, color, color, bgcolor, bgcolor, colspan, EndTime)
                    End If
                Else
                    HTML &= GenTdFourClose(Confirm_Time, color, bgcolor, bgcolor, bgcolor, colspan, EndTime)
                End If


            ElseIf (HourTo < 13) Then
                '7-12
                If HourTo = 12 Then
                    If (MinuteTo < 1) Then
                        HTML &= GenTdFourClose(Confirm_Time, color, color, bgcolor, bgcolor, colspan, EndTime)
                    Else
                        HTML &= GenTdFourClose(Confirm_Time, color, color, color, bgcolor, colspan, EndTime)
                    End If
                Else
                    HTML &= GenTdFourClose(Confirm_Time, color, color, bgcolor, bgcolor, colspan, EndTime)
                End If

            ElseIf (HourTo < 19) Then
                '13-18
                If HourTo = 18 Then
                    If (MinuteTo < 1) Then
                        HTML &= GenTdFourClose(Confirm_Time, color, color, color, bgcolor, colspan, EndTime)
                    Else
                        HTML &= GenTdFourClose(Confirm_Time, color, color, color, color, colspan, EndTime)
                    End If
                Else
                    HTML &= GenTdFourClose(Confirm_Time, color, color, color, bgcolor, colspan, EndTime)
                End If

            Else
                '19-24
                HTML &= GenTdFourClose(Confirm_Time, color, color, color, color, colspan, EndTime)
            End If
            HTML &= "</tr></table>"
            HTML &= "</td>"

        End If

        Return HTML
    End Function

    Private Function GenTdFourOpen(ByVal Confirm As String, ByVal color1 As String, ByVal color2 As String, ByVal color3 As String, ByVal color4 As String, ByVal colspan As Integer, ByVal S As String, ByVal E As String, ByVal D_S As String, ByVal D_E As String) As String
        Dim HTML As String = ""
        Dim Text1 As String = "&nbsp;"
        Dim Text2 As String = "&nbsp;"
        Dim Text3 As String = "&nbsp;"
        Dim Text4 As String = "&nbsp;"
        Dim Style1 As String = ""
        Dim Style2 As String = ""
        Dim Style3 As String = ""
        Dim Style4 As String = ""
        Dim chkNotcomplete As Boolean = False
        Dim chkNotcompleteEnd As Boolean = False
        If (CDate(D_S) < CDate(txtDate.Text)) Then
            S = ""
            If (color1 <> "#edeeed") And (color1 <> "#cbcdcb") Then
                chkNotcomplete = True
            ElseIf (color2 <> "#edeeed") And (color2 <> "#cbcdcb") Then
                color1 = color2
                chkNotcomplete = True
            ElseIf (color3 <> "#edeeed") And (color3 <> "#cbcdcb") Then
                color1 = color3
                color2 = color3
                chkNotcomplete = True
            Else
                color1 = color4
                color2 = color4
                color3 = color4
                chkNotcomplete = True
            End If
        ElseIf (CDate(D_E) > CDate(txtDateTo.Text)) Then
            E = ""
            If (color1 <> "#edeeed") And (color1 <> "#cbcdcb") Then
                color2 = color1
                color3 = color1
                color4 = color1
                chkNotcompleteEnd = True
            ElseIf (color2 <> "#edeeed") And (color2 <> "#cbcdcb") Then
                color3 = color2
                color4 = color2
                chkNotcompleteEnd = True
            ElseIf (color3 <> "#edeeed") And (color3 <> "#cbcdcb") Then
                color4 = color3
                chkNotcompleteEnd = True
            Else
                chkNotcompleteEnd = True
            End If
        End If

        If (colspan = 1) Then
            If (color1 <> "#edeeed") And (color1 <> "#cbcdcb") Then
                Text1 &= S & " " 'เวลาเริ่ม
                If (color2 <> "#edeeed") And (color2 <> "#cbcdcb") Then
                    If (color3 <> "#edeeed") And (color3 <> "#cbcdcb") Then
                        If (color4 <> "#edeeed") And (color4 <> "#cbcdcb") Then
                            Text4 &= E 'เวลาจบ
                        Else
                            Text3 &= E 'เวลาจบ
                        End If
                    Else
                        Text2 &= E 'เวลาจบ
                    End If
                Else
                    Text1 &= E 'เวลาจบ
                End If
            Else
                If (color2 <> "#edeeed") And (color2 <> "#cbcdcb") Then
                    Text2 = S & " " 'เวลาเริ่ม
                    If (color3 <> "#edeeed") And (color3 <> "#cbcdcb") Then
                        If (color4 <> "#edeeed") And (color4 <> "#cbcdcb") Then
                            Text4 &= E 'เวลาจบ
                        Else
                            Text3 &= E 'เวลาจบ
                        End If
                    Else
                        Text2 &= E 'เวลาจบ
                    End If
                Else
                    If (color3 <> "#edeeed") And (color3 <> "#cbcdcb") Then
                        Text3 = S & " " 'เวลาเริ่ม
                        If (color4 <> "#edeeed") And (color4 <> "#cbcdcb") Then
                            Text4 &= E 'เวลาจบ
                        Else
                            Text3 &= E 'เวลาจบ
                        End If
                    Else
                        Text4 &= S & " " & E 'เวลาเริ่ม - เวลาจบ
                    End If

                End If
            End If
        Else
            If (color1 <> "#edeeed") And (color1 <> "#cbcdcb") Then
                Text1 &= S 'เวลาเริ่ม
            ElseIf (color2 <> "#edeeed") And (color2 <> "#cbcdcb") Then
                Text2 &= S 'เวลาเริ่ม
            ElseIf (color3 <> "#edeeed") And (color3 <> "#cbcdcb") Then
                Text3 &= S 'เวลาเริ่ม
            Else
                Text4 &= S 'เวลาเริ่ม
            End If
        End If
        Dim chkconfirm As Boolean = True
        If (Confirm = "Y") Then
            If (color1 = "#edeeed") Then
                color1 = "../Image/Empty_Row.png"
            ElseIf (color1 = "#cbcdcb") Then
                color1 = "../Image/Empty_Row2.png"
            Else
                If chkNotcomplete = False Then
                    Style1 = "border-left-style: solid; border-left-width: medium;border-left-color: #000000;"
                    chkconfirm = False
                End If

            End If
            If (color2 = "#edeeed") Or (color2 = "#cbcdcb") Then
                If (color2 = "#cbcdcb") Then
                    color2 = "../Image/Empty_Row2.png"
                Else
                    color2 = "../Image/Empty_Row.png"
                End If

                If (color1 <> "../Image/Empty_Row2.png" And color1 <> "../Image/Empty_Row.png") Then
                    If Not (chkconfirm) Then
                        If chkNotcompleteEnd = False Then
                            Style1 = "border-right-style: solid; border-right-width: medium;border-right-color: #000000;"
                            chkconfirm = True
                            chkNotcompleteEnd = True
                        End If
                    ElseIf (colspan = 1) Then
                        If chkNotcompleteEnd = False Then
                            Style1 = "border-right-style: solid; border-right-width: medium;border-right-color: #000000;"
                            chkNotcompleteEnd = True
                        End If

                    End If
                End If

            Else
                If (chkconfirm) Then
                    If chkNotcomplete = False Then
                        Style2 = "border-left-style: solid; border-left-width: medium;border-left-color: #000000;"
                        chkconfirm = False
                    End If

                End If
            End If
            If (color3 = "#edeeed") Or (color3 = "#cbcdcb") Then
                If (color3 = "#cbcdcb") Then
                    color3 = "../Image/Empty_Row2.png"
                Else
                    color3 = "../Image/Empty_Row.png"
                End If
                If (color2 <> "../Image/Empty_Row2.png" And color2 <> "../Image/Empty_Row.png") Then
                    If Not (chkconfirm) Then
                        If chkNotcompleteEnd = False Then
                            Style2 = "border-right-style: solid; border-right-width: medium;border-right-color: #000000;"
                            chkconfirm = True
                            chkNotcompleteEnd = True
                        End If
                    ElseIf (colspan = 1) Then
                        If chkNotcompleteEnd = False Then
                            Style2 = "border-right-style: solid; border-right-width: medium;border-right-color: #000000;"
                            chkNotcompleteEnd = True
                        End If

                    End If
                End If

            Else
                If (chkconfirm) Then
                    If chkNotcomplete = False Then
                        Style3 = "border-left-style: solid; border-left-width: medium;border-left-color: #000000;"
                        chkconfirm = False
                    End If

                End If
            End If
            If (color4 = "#edeeed") Or (color4 = "#cbcdcb") Then
                If (color4 = "#cbcdcb") Then
                    color4 = "../Image/Empty_Row2.png"
                Else
                    color4 = "../Image/Empty_Row.png"
                End If
                If (color3 <> "../Image/Empty_Row2.png" And color3 <> "../Image/Empty_Row.png") Then
                    If Not (chkconfirm) Then
                        If chkNotcompleteEnd = False Then
                            Style3 = "border-right-style: solid; border-right-width: medium;border-right-color: #000000;"
                            chkNotcompleteEnd = True
                        End If
                    ElseIf (colspan = 1) Then
                        If chkNotcompleteEnd = False Then
                            Style3 = "border-right-style: solid; border-right-width: medium;border-right-color: #000000;"
                            chkNotcompleteEnd = True
                        End If

                    End If
                End If

            Else
                If (chkconfirm) Then
                    If chkNotcomplete = False Then
                        Style4 = "border-left-style: solid; border-left-width: medium;border-left-color: #000000;"
                    ElseIf colspan = 1 Then
                        Style4 = "border-right-style: solid; border-right-width: medium;border-right-color: #000000;"
                        chkNotcompleteEnd = True
                    End If
                ElseIf (colspan = 1) Then
                    If chkNotcompleteEnd = False Then
                        Style4 = "border-right-style: solid; border-right-width: medium;border-right-color: #000000;"
                    End If

                End If


            End If
        Else
            If (color1 = "#edeeed") Then
                color1 = "../Image/Empty_Row.png"
            ElseIf (color1 = "#cbcdcb") Then
                color1 = "../Image/Empty_Row2.png"
            End If
            If (color2 = "#edeeed") Then
                color2 = "../Image/Empty_Row.png"
            ElseIf (color2 = "#cbcdcb") Then
                color2 = "../Image/Empty_Row2.png"
            End If
            If (color3 = "#edeeed") Then
                color3 = "../Image/Empty_Row.png"
            ElseIf (color3 = "#cbcdcb") Then
                color3 = "../Image/Empty_Row2.png"
            End If
            If (color4 = "#edeeed") Then
                color4 = "../Image/Empty_Row.png"
            ElseIf (color4 = "#cbcdcb") Then
                color4 = "../Image/Empty_Row2.png"
            End If
        End If

        HTML &= "<td align='left' width='35px' height='24px' style='background-image:url(" & color1 & ");background-repeat:repeat;cursor:pointer;" & Style1 & "'><font size='1'>" & Text1 & "</font></td>"
        HTML &= "<td align='left' width='35px' height='24px' style='background-image:url(" & color2 & ");background-repeat:repeat;cursor:pointer;" & Style2 & "'><font size='1'>" & Text2 & "</font></td>"
        HTML &= "<td align='left' width='35px' height='24px' style='background-image:url(" & color3 & ");background-repeat:repeat;cursor:pointer;" & Style3 & "'><font size='1'>" & Text3 & "</font></td>"
        HTML &= "<td align='left' width='35px' height='24px' style='background-image:url(" & color4 & ");background-repeat:repeat;cursor:pointer;" & Style4 & "'><font size='1'>" & Text4 & "</font></td>"

        Return HTML
    End Function

    Private Function GenTdFourClose(ByVal Confirm As String, ByVal color1 As String, ByVal color2 As String, ByVal color3 As String, ByVal color4 As String, ByVal colspan As Integer, ByVal E As String) As String
        Dim HTML As String = ""
        Dim Text1 As String = "&nbsp;"
        Dim Text2 As String = "&nbsp;"
        Dim Text3 As String = "&nbsp;"
        Dim Text4 As String = "&nbsp;"
        Dim Style1 As String = ""
        Dim Style2 As String = ""
        Dim Style3 As String = ""
        Dim Style4 As String = ""
        If (Confirm = "Y") Then
            Dim chkConfirm As Boolean = True
            If (color2 = "#edeeed") Or (color2 = "#cbcdcb") Then
                If (color2 = "#cbcdcb") Then
                    color2 = "../Image/Empty_Row2.png"
                Else
                    color2 = "../Image/Empty_Row.png"
                End If

                If (chkConfirm) Then
                    Text1 = E
                    Style1 = "border-right-style: solid; border-right-width: medium;border-right-color: #000000;"
                    chkConfirm = False
                End If
            End If
            If (color3 = "#edeeed") Or (color3 = "#cbcdcb") Then
                If (color3 = "#cbcdcb") Then
                    color3 = "../Image/Empty_Row2.png"
                Else
                    color3 = "../Image/Empty_Row.png"
                End If

                If (chkConfirm) Then
                    Text2 = E
                    Style2 = "border-right-style: solid; border-right-width: medium;border-right-color: #000000;"
                    chkConfirm = False
                End If
            End If
            If (color4 = "#edeeed") Or (color4 = "#cbcdcb") Then
                If (color4 = "#cbcdcb") Then
                    color4 = "../Image/Empty_Row2.png"
                Else
                    color4 = "../Image/Empty_Row.png"
                End If

                If (chkConfirm) Then
                    Text3 = E
                    Style3 = "border-right-style: solid; border-right-width: medium;border-right-color: #000000;"

                End If
            Else
                If (chkConfirm) Then
                    Text4 = E

                    If (E <> "") Then
                        Style4 = "border-right-style: solid; border-right-width: medium;border-right-color: #000000;"
                    End If
                End If
            End If
        Else
            Dim chkEnd As Boolean = True
            If (color1 = "#edeeed") Then
                color1 = "../Image/Empty_Row.png"
            ElseIf (color1 = "#cbcdcb") Then
                color1 = "../Image/Empty_Row2.png"
            End If
            If (color2 = "#edeeed") Or (color2 = "#cbcdcb") Then
                If (color2 = "#cbcdcb") Then
                    color2 = "../Image/Empty_Row2.png"
                Else
                    color2 = "../Image/Empty_Row.png"
                End If

                If (chkEnd) Then
                    Text1 = E
                    chkEnd = False
                End If
            End If
            If (color3 = "#edeeed") Or (color3 = "#cbcdcb") Then
                If (color3 = "#cbcdcb") Then
                    color3 = "../Image/Empty_Row2.png"
                Else
                    color3 = "../Image/Empty_Row.png"
                End If

                If (chkEnd) Then
                    Text2 = E
                    chkEnd = False
                End If
            End If
            If (color4 = "#edeeed") Or (color4 = "#cbcdcb") Then
                If (color4 = "#cbcdcb") Then
                    color4 = "../Image/Empty_Row2.png"
                Else
                    color4 = "../Image/Empty_Row.png"
                End If

                If (chkEnd) Then
                    Text3 = E
                End If
            Else
                If (chkEnd) Then
                    Text4 = E
                End If
            End If
        End If

        HTML &= "<td align='right' width='35px' height='24px' style='background-image:url(" & color1 & ");background-repeat:repeat;cursor:pointer;" & Style1 & "'><font size='1'>" & Text1 & "</font></td>"
        HTML &= "<td align='right' width='35px' height='24px' style='background-image:url(" & color2 & ");background-repeat:repeat;cursor:pointer;" & Style2 & "'><font size='1'>" & Text2 & "</font></td>"
        HTML &= "<td align='right' width='35px' height='24px' style='background-image:url(" & color3 & ");background-repeat:repeat;cursor:pointer;" & Style3 & "'><font size='1'>" & Text3 & "</font></td>"
        HTML &= "<td align='right' width='35px' height='24px' style='background-image:url(" & color4 & ");background-repeat:repeat;cursor:pointer;" & Style4 & "'><font size='1'>" & Text4 & "</font></td>"


        Return HTML
    End Function

    Private Function GenComment(ByVal Left As String, ByVal Top As String, ByVal str As String) As String
        Dim XHTML As String = ""
        XHTML &= "<table style='z-index:100;position:absolute;left:" & Left & "px;top:" & Top & "px;'  border='0' cellpadding='0' cellspacing='1' >"
        XHTML &= "<tr>"
        XHTML &= "<td NOWRAP><font class='White-Style' style='color:#000000'>" & str & "</font></td>"
        XHTML &= "</tr>"
        XHTML &= "</table>"
        Return XHTML
    End Function


    Private Function GetDayinMonth(ByVal Month, ByVal Year) As Integer
        Return Date.DaysInMonth(Year, Month)
    End Function

    Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        If (Timer1.Interval = 1000) Then
            Timer1.Interval = 600000
        End If
        GenHTML_NewVersion()
    End Sub

    Private Function GetBerth_Depart(ByVal Booking As String, ByVal Berth As String, ByVal Depart As String, ByRef D_From As String, ByRef D_To As String, ByRef T_From As String, ByRef T_To As String) As String
        'คำนวน Berth Depart
        Dim dtLog As DataTable = clsGet.Get_Booking_LogSheet_In(Booking)
        If (dtLog.Rows.Count > 0) Then
            If (dtLog.Rows(0).Item("Berthing_Date") & "" <> "") Then
                Dim Berthing_Date As String = dtLog.Rows(0).Item("Berthing_Date") & ""
                Dim Berthing_Time As String = dtLog.Rows(0).Item("Berthing_TimeTo") & ""
                Try
                    If (CInt(dtLog.Rows(0).Item("Berthing_TimeTo").ToString().Split(":")(0)) < CInt(dtLog.Rows(0).Item("Berthing_TimeFrom").ToString.Split(":")(0))) Then
                        Berthing_Date = CDate(Berthing_Date).AddDays(1).ToString("dd MMM", New CultureInfo("en-US")) & " " & Berthing_Date.Split(" ")(2)
                    End If
                Catch ex As Exception

                End Try
                If (Berthing_Date.Trim().Length < 9) Then
                    Try
                        Dim day As String = Berthing_Date.Substring(0, 2)
                        Dim Month As String = GetNameMonth(Berthing_Date.Substring(3, 2))
                        Dim Year As String = "20" & Berthing_Date.Substring(6, 2)
                        Berthing_Date = day & " " & Month & " " & Year
                    Catch ex As Exception

                    End Try
                End If
                Berth = Berthing_Date & " " & dtLog.Rows(0).Item("Berthing_TimeTo") & ""
                D_From = Berthing_Date & ""
                T_From = dtLog.Rows(0).Item("Berthing_TimeTo") & ""
                dtLog = clsGet.Get_Booking_LogSheet_Out(Booking)
                If dtLog.Rows.Count > 0 Then
                    If (dtLog.Rows(0).Item("Last_Date") & "" <> "") Then
                        D_To = dtLog.Rows(0).Item("Last_Date") & ""
                        If (D_To.Trim().Length < 9) Then
                            Try
                                Dim day As String = D_To.Substring(0, 2)
                                Dim Month As String = GetNameMonth(D_To.Substring(3, 2))
                                Dim Year As String = "20" & D_To.Substring(6, 2)
                                D_To = day & " " & Month & " " & Year
                            Catch ex As Exception

                            End Try
                        End If
                        Depart = D_To & " " & dtLog.Rows(0).Item("Last_Time") & ""
                        T_To = dtLog.Rows(0).Item("Last_Time") & ""
                    Else
                        'คำนวน Standard Time
                        'dtLog = clsGet.Get_Booking_ProductOperationBulk(Booking)
                        'Dim Before As String = "00:00"
                        'Dim After As String = "00:00"
                        'Dim PlusTank As String = "00:00"
                        'Dim StardardTime As Integer = 0
                        'Dim First As Boolean = True
                        'Dim Product_Event As String = clsGet.Get_ProductEvent(Booking)
                        'For Each dr As DataRow In dtLog.Rows
                        '    Dim dsProduct As DataTable
                        '    If (ddlWharf.SelectedValue = "L") Then
                        '        If (Product_Event <> "") Then
                        '            If (Product_Event = "L") Then
                        '                dsProduct = clsGet.Get_StardardTime("'" & dr.Item("Product_Name") & "'", "L", "")
                        '            Else
                        '                dsProduct = clsGet.Get_StardardTime("'" & dr.Item("Product_Name") & "'", "D", "")
                        '            End If
                        '            If (dsProduct.Rows.Count > 0) Then
                        '                If (dsProduct.Rows(0).Item("Rate") & "" <> "") Then
                        '                    If (First) Then
                        '                        First = False
                        '                        Before = dsProduct.Rows(0).Item("Before") & ""
                        '                        PlusTank = dsProduct.Rows(0).Item("Plus_Tank") & ""
                        '                    End If
                        '                    After = dsProduct.Rows(0).Item("After") & ""
                        '                    StardardTime = StardardTime + (CInt(dr.Item("Product_Amount"))) / CInt(dsProduct.Rows(0).Item("Rate"))
                        '                End If
                        '            End If

                        '        End If

                        '    Else
                        '        'Bulk
                        '        dsProduct = clsGet.Get_StardardTimeBulk("'" & dr.Item("Product_name") & "'", "E")

                        '        If (dsProduct.Rows(0).Item("StandardTime") & "" <> "") Then
                        '            StardardTime = StardardTime + (CInt(dr.Item("Product_Amount")) / (CInt(dsProduct.Rows(0).Item("StandardTime")) / 24))
                        '        End If
                        '    End If
                        'Next

                        'If (Before <> "") Then
                        '    Try
                        '        StardardTime = StardardTime + CInt(Before.Split(":")(0)) '+ CInt(Before.Split(":")(1))
                        '    Catch ex As Exception

                        '    End Try
                        'End If
                        'If (PlusTank <> "") Then
                        '    Try
                        '        StardardTime = StardardTime + CInt(PlusTank)
                        '    Catch ex As Exception

                        '    End Try
                        'End If
                        'If (After <> "") Then
                        '    Try
                        '        StardardTime = StardardTime + CInt(After.Split(":")(0)) '+ CInt(After.Split(":")(1))
                        '    Catch ex As Exception

                        '    End Try
                        'End If

                        'Try
                        '    Dim Hour As Integer = Berthing_Time.Split(":")(0) + StardardTime
                        '    Dim PlusDay As Integer = 0
                        '    If (Hour > 23) Then
                        '        PlusDay = Hour / 24
                        '        Hour = Hour Mod 24
                        '    End If
                        '    Depart = CDate(Berthing_Date).AddDays(PlusDay).ToString("dd MMM", New CultureInfo("en-US")) & " " & Berthing_Date.Split(" ")(2) & " " & Hour.ToString("00") & ":" & Berthing_Time.Split(":")(1)
                        '    D_To = CDate(Berthing_Date).AddDays(PlusDay).ToString("dd MMM", New CultureInfo("en-US")) & " " & Berthing_Date.Split(" ")(2) & ""
                        '    T_To = Hour.ToString("00") & ":" & Berthing_Time.Split(":")(1)
                        'Catch ex As Exception

                        'End Try

                    End If
                Else
                    'คำนวน Standard Time

                    'dtLog = clsGet.Get_Booking_ProductOperationBulk(Booking)
                    'Dim Before As String = "00:00"
                    'Dim After As String = "00:00"
                    'Dim PlusTank As String = "00:00"
                    'Dim StardardTime As Integer = 0
                    'Dim First As Boolean = True
                    'Dim Product_Event As String = clsGet.Get_ProductEvent(Booking)
                    'For Each dr As DataRow In dtLog.Rows
                    '    Dim dsProduct As DataTable
                    '    If (ddlWharf.SelectedValue & "" = "L") Then
                    '        If (Product_Event <> "") Then
                    '            If (Product_Event = "L") Then
                    '                dsProduct = clsGet.Get_StardardTime("'" & dr.Item("Product_Name") & "'", "L", "")
                    '            Else
                    '                dsProduct = clsGet.Get_StardardTime("'" & dr.Item("Product_Name") & "'", "D", "")
                    '            End If
                    '            If (dsProduct.Rows.Count > 0) Then
                    '                If (dsProduct.Rows(0).Item("Rate") & "" <> "") Then
                    '                    If (First) Then
                    '                        First = False
                    '                        Before = dsProduct.Rows(0).Item("Before") & ""
                    '                        PlusTank = dsProduct.Rows(0).Item("Plus_Tank") & ""
                    '                    End If
                    '                    After = dsProduct.Rows(0).Item("After") & ""
                    '                    StardardTime = StardardTime + (CInt(dr.Item("Product_Amount"))) / CInt(dsProduct.Rows(0).Item("Rate"))
                    '                End If
                    '            End If

                    '        End If

                    '    Else
                    '        'Bulk
                    '        dsProduct = clsGet.Get_StardardTimeBulk("'" & dr.Item("Product_name") & "'", "E")

                    '        If (dsProduct.Rows(0).Item("StandardTime") & "" <> "") Then
                    '            StardardTime = StardardTime + (CInt(dr.Item("Product_Amount")) / (CInt(dsProduct.Rows(0).Item("StandardTime")) / 24))
                    '        End If
                    '    End If
                    'Next

                    'If (Before <> "") Then
                    '    Try
                    '        StardardTime = StardardTime + CInt(Before.Split(":")(0)) '+ CInt(Before.Split(":")(1))
                    '    Catch ex As Exception

                    '    End Try
                    'End If
                    'If (PlusTank <> "") Then
                    '    Try
                    '        StardardTime = StardardTime + CInt(PlusTank)
                    '    Catch ex As Exception

                    '    End Try
                    'End If
                    'If (After <> "") Then
                    '    Try
                    '        StardardTime = StardardTime + CInt(After.Split(":")(0)) '+ CInt(After.Split(":")(1))
                    '    Catch ex As Exception

                    '    End Try
                    'End If

                    'Try
                    '    Dim Hour As Integer = Berthing_Time.Split(":")(0) + StardardTime
                    '    Dim PlusDay As Integer = 0
                    '    If (Hour > 23) Then
                    '        PlusDay = Hour / 24
                    '        Hour = Hour Mod 24
                    '    End If
                    '    Depart = CDate(Berthing_Date).AddDays(PlusDay).ToString("dd MMM", New CultureInfo("en-US")) & " " & Berthing_Date.Split(" ")(2) & " " & Hour.ToString("00") & ":" & Berthing_Time.Split(":")(1)
                    '    D_To = CDate(Berthing_Date).AddDays(PlusDay).ToString("dd MMM", New CultureInfo("en-US")) & " " & Berthing_Date.Split(" ")(2) & ""
                    '    T_To = Hour.ToString("00") & ":" & Berthing_Time.Split(":")(1)
                    'Catch ex As Exception

                    'End Try
                End If
            End If
        End If
        If (D_From.Trim().Length < 9) Then
            Try
                Dim day As String = D_From.Substring(0, 2)
                Dim Month As String = GetNameMonth(D_From.Substring(3, 2))
                Dim Year As String = "20" & D_From.Substring(6, 2)
                D_From = day & " " & Month & " " & Year
            Catch ex As Exception

            End Try
        End If
        Return Berth & "-" & Depart
    End Function

    Private Function GetNameMonth(ByVal Month As String) As String
        Try
            Select Case (CInt(Month).ToString("00"))
                Case "01"
                    Return "Jan"
                Case "02"
                    Return "Feb"
                Case "03"
                    Return "Mar"
                Case "04"
                    Return "Apr"
                Case "05"
                    Return "May"
                Case "06"
                    Return "Jun"
                Case "07"
                    Return "Jul"
                Case "08"
                    Return "Aug"
                Case "09"
                    Return "Sep"
                Case "10"
                    Return "Oct"
                Case "11"
                    Return "Nov"
                Case "12"
                    Return "Dec"
                Case Else
                    Return ""
            End Select
        Catch ex As Exception
            Return Month
        End Try

    End Function

    Private Sub MainPage_PreInit(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreInit
        Dim ds As DataSet = Session("UserData")
        If Not (ds Is Nothing) Then
            Page.MasterPageFile = clsGet.GetMaster_Page(ds.Tables(0).Rows(0).Item("User_Name").ToString(), "M")
        End If
    End Sub

    Private Function GetColumnExcel(ByVal Value As Integer) As String
        Dim Number As Integer = 0
        Dim strT As String = ""
        Dim txt As String = ""
        Dim divN As Integer = Value \ 26
        Dim ModN As Integer = Value Mod 26
        Dim divA As Integer = divN \ 26
        If (ModN = 0) And (divN > 0) Then
            divN -= 1
        End If
        If (Value > 26) Then
            For i As Integer = 1 To divA + 1
                Select Case (divN)
                    Case "1" : strT = "A"
                    Case "2" : strT = "B"
                    Case "3" : strT = "C"
                    Case "4" : strT = "D"
                    Case "5" : strT = "E"
                    Case "6" : strT = "F"
                    Case "7" : strT = "G"
                    Case "8" : strT = "H"
                    Case "9" : strT = "I"
                    Case "10" : strT = "J"
                    Case "11" : strT = "K"
                    Case "12" : strT = "L"
                    Case "13" : strT = "M"
                    Case "14" : strT = "N"
                    Case "15" : strT = "O"
                    Case "16" : strT = "P"
                    Case "17" : strT = "Q"
                    Case "18" : strT = "R"
                    Case "19" : strT = "S"
                    Case "20" : strT = "T"
                    Case "21" : strT = "U"
                    Case "22" : strT = "V"
                    Case "23" : strT = "W"
                    Case "24" : strT = "X"
                    Case "25" : strT = "Y"
                    Case "0" : strT = "Z"
                End Select
                txt &= strT
            Next
        End If
        Select Case (ModN)
            Case "1" : strT = "A"
            Case "2" : strT = "B"
            Case "3" : strT = "C"
            Case "4" : strT = "D"
            Case "5" : strT = "E"
            Case "6" : strT = "F"
            Case "7" : strT = "G"
            Case "8" : strT = "H"
            Case "9" : strT = "I"
            Case "10" : strT = "J"
            Case "11" : strT = "K"
            Case "12" : strT = "L"
            Case "13" : strT = "M"
            Case "14" : strT = "N"
            Case "15" : strT = "O"
            Case "16" : strT = "P"
            Case "17" : strT = "Q"
            Case "18" : strT = "R"
            Case "19" : strT = "S"
            Case "20" : strT = "T"
            Case "21" : strT = "U"
            Case "22" : strT = "V"
            Case "23" : strT = "W"
            Case "24" : strT = "X"
            Case "25" : strT = "Y"
            Case "0" : strT = "Z"
        End Select
        If (Value = 52) Then
            Dim xx = "SSS"
        End If
        txt &= strT
        Return txt
    End Function

    Private Sub GenExcel()
        Try
            If ((txtDate.Text.Trim() = "") Or (txtDateTo.Text.Trim() = "")) Then
                ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", " alert('กรุณาทำการระบุช่วงการแสดงผล !!');", True)
                Exit Sub
            End If
            If (CDate(txtDate.Text) > CDate(txtDateTo.Text)) Then
                ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", " alert('ช่วงระยะเวลาการแสดงผลไม่ถูกต้อง!!');", True)
                Exit Sub
            End If
            Dim GuidID As String            
            GuidID = Guid.NewGuid.ToString()
            Dim FileName As String = "../Uploads/" & GuidID & ".xls"
            'Dim FileName As String = GuidID & ".xls"

            '*** Create Excel.Application ***'
            Dim xlApp As New Excel.Application
            xlApp.Visible = False
            xlApp.UserControl = True
            Dim xlSheet1 As Excel.Worksheet
            Dim xlBook As Excel.Workbook
            System.Threading.Thread.CurrentThread.CurrentCulture = System.Globalization.CultureInfo.CreateSpecificCulture("en-US")
            xlBook = xlApp.Workbooks.Add

            xlBook.Application.Visible = False

            '*** Create Sheet 1 ***'
            xlSheet1 = xlBook.Worksheets(1)
            xlSheet1.Name = "Ship Schedule"

            '*** Header ***'

            Dim StartTime As String = CDate(txtDate.Text).ToString("yyyyMMdd")  'วันเริ่มต้น
            Dim EndTime As String = CDate(txtDateTo.Text).ToString("yyyyMMdd")  'วันสิ้นสุด
            Dim dsPORT As DataTable = clsGet.Get_WharfAll(ddlWharf.SelectedValue)
            Dim XHTML As String = ""
            Dim XHTML_Comment As String = ""
            Dim top As Integer = 54
            Dim Day As Integer = DateDiff(DateInterval.Day, CDate(txtDate.Text), CDate(txtDateTo.Text))

            Dim DayinMount As Integer = GetDayinMonth(CDate(txtDate.Text).Month.ToString("00"), CDate(txtDate.Text).Year)
            Dim DayDiff As Integer = 0
            If (CDate(txtDate.Text).Month.ToString("00") = CDate(txtDateTo.Text).Month.ToString("00")) Then
                DayDiff = CDate(txtDateTo.Text).Day - CDate(txtDate.Text).Day + 1
            Else
                DayDiff = DayinMount - CDate(txtDate.Text).Day + 1
            End If

            Dim Month As Integer = 0
            'Header
            xlSheet1.Range("A1", "A2").MergeCells = True
            xlSheet1.Range("A1", "A2").Borders.Weight = 2
            xlApp.ActiveSheet.Cells(1, 1).Value = "Wf."
            xlSheet1.Range("A1").HorizontalAlignment = HorizontalAlign.Center
            xlSheet1.Range("A1").VerticalAlignment = VerticalAlign.Middle

            Dim chk As Boolean = True
            Dim COunt As Integer = 1
            Dim daycal As Integer = Day - DayDiff
            Dim chkManyMonth As Boolean = True
            If (daycal < 0) Then
                daycal = DayDiff - Day
                chkManyMonth = False
            End If
            Dim RemeberCOlumn As Integer = 2
            While (chk)
                If (Month = 0) Then
                    ' XHTML &= "<td class='LockedColumnForTable2' align='center' colspan='" & DayDiff & "' height='24px' style='background-color:#ddffce;'  ><b><font class='White-Style' style='color:#000000'>&nbsp;" & CDate(txtDate.Text).ToString("MMMM", New CultureInfo("en-US")) & " " & CDate(txtDate.Text).ToString("yyyy") & "</font></b></td>"
                    Dim MergeColumn As String = GetColumnExcel((RemeberCOlumn - 1) + DayDiff) & "1"
                    Dim MergeSource As String = GetColumnExcel(RemeberCOlumn) & "1"
                    xlSheet1.Range(MergeSource, MergeColumn).MergeCells = True
                    xlSheet1.Range(MergeSource, MergeColumn).Borders.Weight = 2
                    xlApp.ActiveSheet.Cells(1, RemeberCOlumn).Value = CDate(txtDate.Text).ToString("MMMM", New CultureInfo("en-US")) & " " & CDate(txtDate.Text).ToString("yyyy")
                    xlSheet1.Range(GetColumnExcel(RemeberCOlumn) & 1).HorizontalAlignment = HorizontalAlign.Center
                Else
                    ' XHTML &= "<td class='LockedColumnForTable2' align='center' colspan='" & DayDiff & "' height='24px' style='background-color:#ddffce;'  ><b><font class='White-Style' style='color:#000000'>&nbsp;" & CDate(txtDate.Text).AddMonths(Month).ToString("MMMM", New CultureInfo("en-US")) & " " & CDate(txtDate.Text).AddMonths(Month).ToString("yyyy") & "</font></b></td>"
                    Dim MergeColumn As String = GetColumnExcel((RemeberCOlumn - 1) + DayDiff) & "1"
                    Dim MergeSource As String = GetColumnExcel(RemeberCOlumn) & "1"
                    xlSheet1.Range(MergeSource, MergeColumn).MergeCells = True
                    xlSheet1.Range(MergeSource, MergeColumn).Borders.Weight = 2
                    xlApp.ActiveSheet.Cells(1, RemeberCOlumn).Value = CDate(txtDate.Text).AddMonths(Month).ToString("MMMM", New CultureInfo("en-US")) & " " & CDate(txtDate.Text).AddMonths(Month).ToString("yyyy")
                    xlSheet1.Range(GetColumnExcel(RemeberCOlumn) & 1).HorizontalAlignment = HorizontalAlign.Center
                End If

                DayinMount = GetDayinMonth(CDate(txtDate.Text).AddMonths(COunt).Month.ToString("00"), CDate(txtDate.Text).AddMonths(COunt).Year)
                Month += 1
                If (DayinMount >= daycal) Then
                    'จบเดือน
                    If (chkManyMonth) Then
                        ' XHTML &= "<td class='LockedColumnForTable2' align='center' colspan='" & daycal + 1 & "' height='24px' style='background-color:#ddffce;'  ><b><font class='White-Style' style='color:#000000'>&nbsp;" & CDate(txtDate.Text).AddMonths(Month).ToString("MMMM", New CultureInfo("en-US")) & " " & CDate(txtDate.Text).AddMonths(Month).ToString("yyyy") & "</font></b></td>"
                        Dim MergeColumn As String = GetColumnExcel(((RemeberCOlumn) + DayDiff) + daycal) & "1"
                        Dim MergeSource As String = GetColumnExcel((RemeberCOlumn) + DayDiff) & "1"
                        xlSheet1.Range(MergeSource, MergeColumn).MergeCells = True
                        xlSheet1.Range(MergeSource, MergeColumn).Borders.Weight = 2
                        xlApp.ActiveSheet.Cells(1, (RemeberCOlumn) + DayDiff).Value = CDate(txtDate.Text).AddMonths(Month).ToString("MMMM", New CultureInfo("en-US")) & " " & CDate(txtDate.Text).AddMonths(Month).ToString("yyyy")
                        xlSheet1.Range(GetColumnExcel((RemeberCOlumn) + DayDiff) & 1).HorizontalAlignment = HorizontalAlign.Center
                    End If
                    chk = False
                Else
                    RemeberCOlumn = RemeberCOlumn + DayDiff
                    DayDiff = DayinMount
                    daycal = daycal - DayDiff
                End If
                COunt += 1
            End While
            DayinMount = GetDayinMonth(CDate(txtDate.Text).Month.ToString("00"), CDate(txtDate.Text).Year)
            daycal = CDate(txtDate.Text).Day
            Month = 1
            Dim DayAfter As String = ""
            For i As Integer = 0 To Day
                Dim txtday As String = ""
                If (daycal > DayinMount) Then
                    daycal = 1
                    DayinMount = GetDayinMonth(CDate(txtDate.Text).AddMonths(Month).Month.ToString("00"), CDate(txtDate.Text).AddMonths(Month).Year)
                    Month += 1
                End If
                If (CDate(daycal & " " & CDate(txtDate.Text).AddMonths(Month - 1).ToString("MMM") & " " & CDate(txtDate.Text).AddMonths(Month - 1).Year).ToString("ddd").ToUpper() = "SAT") Or (CDate(daycal & " " & CDate(txtDate.Text).AddMonths(Month - 1).ToString("MMM") & " " & CDate(txtDate.Text).AddMonths(Month - 1).Year).ToString("ddd").ToUpper() = "SUN") Then
                    txtday = "Sat"
                ElseIf (clsGet.Get_Holiday(daycal.ToString(), CDate(txtDate.Text).Month.ToString("00"), CDate(txtDate.Text).Year)) Then
                    txtday = "Sat"
                ElseIf (daycal & " " & CDate(txtDate.Text).AddMonths(Month - 1).ToString("MMM") & " " & CDate(txtDate.Text).AddMonths(Month - 1).Year = Date.Now.ToString("dd MMM yyyy", New CultureInfo("en-US"))) Then
                    txtday = "Current"
                End If
                If (txtday = "Current") Then
                    'XHTML &= "<td class='LockedColumnForTable2' align='center' height='24px' style='width:140px;background-color:#5e9df4;' ><b><font class='White-Style' style='color:#000000'>&nbsp;" & daycal.ToString("00") & "</font></b></td>"
                    Dim MergeColumn As String = GetColumnExcel(i + 2) & "2"

                    xlSheet1.Range(MergeColumn, MergeColumn).Borders.Weight = 2
                    xlApp.ActiveSheet.Cells(2, i + 2).Interior.ColorIndex = 41
                    xlApp.ActiveSheet.Cells(2, i + 2).Value = daycal.ToString("00")
                ElseIf (txtday = "Sat") Or (txtday = "Sun") Then
                    ' XHTML &= "<td class='LockedColumnForTable2' align='center' height='24px' style='width:140px;background-color:#f7f4a4;' ><b><font class='White-Style' style='color:#000000'>&nbsp;" & daycal.ToString("00") & "</font></b></td>"
                    Dim MergeColumn As String = GetColumnExcel(i + 2) & "2"

                    xlSheet1.Range(MergeColumn, MergeColumn).Borders.Weight = 2
                    xlApp.ActiveSheet.Cells(2, i + 2).Interior.ColorIndex = 36
                    xlApp.ActiveSheet.Cells(2, i + 2).Value = daycal.ToString("00")
                Else
                    ' XHTML &= "<td class='LockedColumnForTable2' align='center' height='24px' style='width:140px;background-color:#ddffce;' ><b><font class='White-Style' style='color:#000000'>&nbsp;" & daycal.ToString("00") & "</font></b></td>"
                    Dim MergeColumn As String = GetColumnExcel(i + 2) & "2"

                    xlSheet1.Range(MergeColumn, MergeColumn).Borders.Weight = 2
                    xlApp.ActiveSheet.Cells(2, i + 2).Interior.ColorIndex = 2
                    xlApp.ActiveSheet.Cells(2, i + 2).Value = daycal.ToString("00")
                End If

                daycal += 1
            Next

            '********************************************************
            'Body 
            '**********************************************************
            Dim row = 0
            Dim startRow As Integer = 3
            For Each dr As DataRow In dsPORT.Rows
                Dim dsQueue As DataSet = clsGet.Get_Booking_WharfQueue(CInt(StartTime - 1), CInt(EndTime + 1), dr.Item("BookingWharf"))
                Dim cQ As Integer = 1
                Dim D_From As String = ""
                Dim D_To As String = ""
                Dim T_From As String = ""
                Dim T_To As String = ""
                Dim dsQTemp As New DataSet
                dsQTemp = dsQueue.Copy
                Dim LoopFOr As Integer = dsQueue.Tables(0).Rows.Count - 1
                For x As Integer = 0 To LoopFOr
                    If (x > LoopFOr) Then
                        Exit For
                    End If
                    D_From = dsQueue.Tables(0).Rows((dsQueue.Tables(0).Rows.Count - 1) - x).Item("D_From") & ""
                    D_To = dsQueue.Tables(0).Rows((dsQueue.Tables(0).Rows.Count - 1) - x).Item("D_To") & ""
                    T_From = dsQueue.Tables(0).Rows((dsQueue.Tables(0).Rows.Count - 1) - x).Item("TimeStart") & ""
                    T_To = dsQueue.Tables(0).Rows((dsQueue.Tables(0).Rows.Count - 1) - x).Item("TimeEnd") & ""
                    GetBerth_Depart(dsQueue.Tables(0).Rows((dsQueue.Tables(0).Rows.Count - 1) - x).Item("Booking_no"), dsQueue.Tables(0).Rows((dsQueue.Tables(0).Rows.Count - 1) - x).Item("D_From") & " " & dsQueue.Tables(0).Rows((dsQueue.Tables(0).Rows.Count - 1) - x).Item("TimeStart"), dsQueue.Tables(0).Rows((dsQueue.Tables(0).Rows.Count - 1) - x).Item("D_To") & " " & dsQueue.Tables(0).Rows((dsQueue.Tables(0).Rows.Count - 1) - x).Item("TimeEnd"), D_From, D_To, T_From, T_To)
                    If (CDate(D_From) < CDate(txtDate.Text)) And (CDate(D_To) < CDate(txtDate.Text)) Then
                        dsQTemp.Tables(0).Rows((dsQueue.Tables(0).Rows.Count - 1) - x).Delete()
                        dsQTemp.AcceptChanges()
                    ElseIf (CDate(D_From) > CDate(txtDateTo.Text)) And (CDate(D_To) > CDate(txtDateTo.Text)) Then
                        dsQTemp.Tables(0).Rows((dsQueue.Tables(0).Rows.Count - 1) - x).Delete()
                        dsQTemp.AcceptChanges()
                    End If
                Next
                dsQueue = dsQTemp
                row += 1
                Dim bgcolorWharf As String = ""
                If ((row Mod 2) = 1) Then
                    bgcolorWharf &= "15"
                Else
                    bgcolorWharf &= "2"
                End If
                If (dsQueue.Tables(0).Rows.Count > 0) Then
                    ' XHTML &= "<tr>"
                    ' XHTML &= "<td  class='LockedColumnForTable'  rowspan='" & dsQueue.Tables(0).Rows.Count * 2 & "' align='center' height='24px' style='width:80px;background-color:" & bgcolorWharf & ";' ><b><font class='White-Style' style='color:#000000'>" & dr.Item("Wharf") & "</font></b></td> "

                    xlSheet1.Range("A" & startRow, "A" & dsQueue.Tables(0).Rows.Count + startRow - 1).MergeCells = True
                    xlSheet1.Range("A" & startRow, "A" & dsQueue.Tables(0).Rows.Count + startRow - 1).Borders.Weight = 2
                    xlApp.ActiveSheet.Cells(startRow, 1).Value = dr.Item("Wharf")
                    xlApp.ActiveSheet.Cells(startRow, 1).Interior.ColorIndex = bgcolorWharf
                    xlSheet1.Range("A" & startRow).HorizontalAlignment = HorizontalAlign.Center
                    xlSheet1.Range("A" & startRow).VerticalAlignment = VerticalAlign.Middle
                    '      startRow = dsQueue.Tables(0).Rows.Count + startRow + 1

                    For Each dq As DataRow In dsQueue.Tables(0).Rows
                        Dim dsBooking As DataSet = clsGet.Get_BookingOneRecord(dq.Item("Booking_no"))
                        Dim DOMESTIC As Boolean = False
                        Try
                            If (dsBooking.Tables(0).Rows(0).Item("Vehicle_Destination") & "" = "I") Then
                                DOMESTIC = True
                            End If
                        Catch ex As Exception

                        End Try

                        Dim Booking As String = dq.Item("Booking_no")
                        D_From = dq.Item("D_From") & ""
                        D_To = dq.Item("D_To") & ""
                        T_From = dq.Item("TimeStart") & ""
                        T_To = dq.Item("TimeEnd") & ""
                        Dim color As String = "#FFFFFF"
                        If (dq.Item("Confirm_Time") & "" = "Y") Then
                            color = "../Image/Empty_Confirm.png"
                        Else
                            color = "#FFFFFF"
                        End If
                        Dim str As String = ""
                        Dim Dateshow As String = dq.Item("D_From").ToString().Substring(0, 2) & "-" & dq.Item("D_To")
                        If (dsBooking.Tables(0).Rows.Count > 0) Then
                            Dateshow = dsBooking.Tables(0).Rows(0).Item("Date_From").ToString().Substring(0, 2) & "-" & dsBooking.Tables(0).Rows(0).Item("Date_To")
                            Dim Product_Event As String = clsGet.Get_ProductEvent(dq.Item("Booking_no"))
                            If (Product_Event = "L") Then
                                str = "(L)"
                                If (dsBooking.Tables(0).Rows(0).Item("Vehicle_Destination") & "" = "O") Then
                                    'color = "#6698FF"
                                    If (dq.Item("Confirm_Time") & "" = "Y") Then
                                        color = "../Image/L_Foreign_confirm.png"
                                    Else
                                        color = "../Image/L_Foreign.png"
                                    End If
                                Else
                                    'color = "#A0CFEC"
                                    If (dq.Item("Confirm_Time") & "" = "Y") Then
                                        color = "../Image/L_Domestic_confirm.png"
                                    Else
                                        color = "../Image/L_Domestic.png"
                                    End If

                                End If
                            ElseIf (Product_Event = "D") Then
                                str = "(D)"
                                If (dsBooking.Tables(0).Rows(0).Item("Vehicle_Destination") & "" = "O") Then
                                    'color = "#347C17"
                                    If (dq.Item("Confirm_Time") & "" = "Y") Then
                                        color = "../Image/D_Foreign_confirm.png"
                                    Else
                                        color = "../Image/D_Foreign.png"
                                    End If

                                Else
                                    'color = "#52D017"
                                    If (dq.Item("Confirm_Time") & "" = "Y") Then
                                        color = "../Image/D_Domestic_confirm.png"
                                    Else
                                        color = "../Image/D_Domestic.png"
                                    End If
                                End If
                            Else
                                If (dq.Item("Confirm_Time") & "" = "Y") Then
                                    color = "../Image/Empty_Confirm.png"
                                Else
                                    color = "#FFFFFF"
                                End If

                            End If
                           
                            Dim dt As DataTable = clsGet.Get_Booking_Product(dq.Item("Booking_no"))
                            Dim firstProduct As Boolean = True
                            Dim Owner_Name As String = ""
                            For Each drt As DataRow In dt.Rows
                                Dim Product_Amount As String = drt.Item("Product_Amount")
                                Dim Product_Unit As String = drt.Item("Product_Unit")
                                If (Product_Unit <> "KB") Then
                                    Dim dtamount As DataTable = clsGet.Get_Load_Unload_Product(dq.Item("Booking_no"), drt.Item("Product_no"))
                                    If (dtamount.Rows.Count > 0) Then
                                        If (ddlWharf.SelectedValue = "B") Then
                                            'Bulk
                                            Product_Amount = dtamount.Rows(0).Item("BU_Truck")
                                        Else
                                            'Liquid
                                            If (Product_Unit = "MT") Then
                                                Product_Amount = dtamount.Rows(0).Item("LU_BLMT")
                                            Else
                                                Product_Amount = dtamount.Rows(0).Item("LU_BL")
                                            End If
                                            If (Product_Amount = "") Then
                                                Product_Amount = "1"
                                            End If
                                            If Product_Unit = "ML" Then
                                                Product_Amount = (CDbl(Product_Amount * 1000) / 1000000).ToString("#######################0.000")
                                            End If
                                        End If
                                    End If


                                End If
                                If (firstProduct) Then
                                    str &= drt.Item("Product_Name") & " ," & Product_Amount & " " & Product_Unit
                                    firstProduct = False
                                Else
                                    str &= "/" & drt.Item("Product_Name") & " ," & drt.Item("Product_Amount") & " " & drt.Item("Product_Unit")
                                End If
                                If (dsBooking.Tables(0).Rows(0).Item("PORT") & "" = "L") Then
                                    'Liquid
                                    If (drt.Item("Product_Event") & "" = "L") Then
                                        If (Owner_Name = "") Then
                                            Owner_Name = drt.Item("Product_Buyer_name") & ""
                                        Else
                                            Owner_Name &= "," & drt.Item("Product_Buyer_name") & ""
                                        End If
                                    Else
                                        If (Owner_Name = "") Then
                                            Owner_Name = drt.Item("Product_Seller_name") & ""
                                        Else
                                            Owner_Name &= "," & drt.Item("Product_Seller_name") & ""
                                        End If
                                    End If
                                Else
                                    'Bulk
                                    If (Owner_Name = "") Then
                                        Owner_Name = drt.Item("Owner_Product_name") & ""
                                    Else
                                        Owner_Name &= "," & drt.Item("Owner_Product_name") & ""
                                    End If
                                End If
                            Next
                            str &= "(" & dsBooking.Tables(0).Rows(0).Item("Vehicle_Text") & ")"
                            If (dsBooking.Tables(0).Rows(0).Item("BunkerFO_Point") & "" <> "0") Or (dsBooking.Tables(0).Rows(0).Item("BunkerDO_Point") & "" <> "0") Then
                                str &= "(Bunker FO " & dsBooking.Tables(0).Rows(0).Item("BunkerFO_Point") & "" & " DO " & dsBooking.Tables(0).Rows(0).Item("BunkerDO_Point") & " )"
                            End If
                            'เพิ่ม เวลา เพิ่ม Agent Remark
                            GetBerth_Depart(dq.Item("Booking_no"), dq.Item("D_From") & " " & dq.Item("TimeStart"), dq.Item("D_To") & " " & dq.Item("TimeEnd"), D_From, D_To, T_From, T_To)
                            str &= Owner_Name
                        End If



                        If (dq.Item("Wharf_Remark") & "" <> "") Then
                            str &= "(" & dq.Item("Wharf_Remark") & ")"
                        End If
                        If (dsBooking.Tables(0).Rows.Count > 0) Then
                            If (dsBooking.Tables(0).Rows(0).Item("Lay_Time") & "" <> "0") Then
                                str &= "(LAYTIME " & dsBooking.Tables(0).Rows(0).Item("Lay_Time") & " Hrs.)"
                            End If
                        End If

                        Dim ColumnEmpty As Integer = DateDiff(DateInterval.Day, CDate((txtDate.Text.Substring(0, txtDate.Text.Length - 5)) & CInt(txtDate.Text.Substring(txtDate.Text.Length - 5, 4))), CDate((D_From.ToString().Substring(0, D_From.Length - 5)) & CInt(D_From.Substring(D_From.Length - 5, 4))))
                        For a As Integer = 0 To ColumnEmpty - 1
                            'XHTML &= "<td align='center' height='24px' style='width:140px;background-color:" & bgcolor & ";' >&nbsp;</td>"
                            xlSheet1.Range(GetColumnExcel(a + 2) & startRow, GetColumnExcel(a + 2) & startRow).Borders.Weight = 2
                            xlApp.ActiveSheet.Cells(startRow, a + 2).Interior.ColorIndex = bgcolorWharf
                        Next
                        Dim colspanColumn As Integer
                        If (CDate(D_From) > CDate(txtDate.Text)) Then
                            colspanColumn = DateDiff(DateInterval.Day, CDate((D_From.Substring(0, D_From.Length - 5)) & CInt(D_From.Substring(D_From.Length - 5, 4))), CDate((D_To.ToString().Substring(0, D_To.Length - 5)) & CInt(D_To.Substring(D_To.Length - 5, 4))))
                        Else
                            colspanColumn = DateDiff(DateInterval.Day, CDate((txtDate.Text.Substring(0, txtDate.Text.Length - 5)) & CInt(txtDate.Text.Substring(txtDate.Text.Length - 5, 4))), CDate((D_To.ToString().Substring(0, D_To.Length - 5)) & CInt(D_To.Substring(D_To.Length - 5, 4))))
                        End If
                        Dim rowNew As Integer = ColumnEmpty
                        If ColumnEmpty < 1 Then
                            rowNew = 0
                        End If
                        str &= "(" & D_From & " " & T_From & " - " & D_To & " " & T_To & ")"
                        If (colspanColumn + ColumnEmpty) >= Day Then
                            ' XHTML &= GenColorTab(dq.Item("Confirm_Time"), (Day - ColumnEmpty) + 1, T_From.Split(":")(0), T_To.Split(":")(0), T_From.Split(":")(1), T_To.Split(":")(1), color, dsBooking, Dateshow, T_From, T_To, D_From, D_To, row, Booking)
                            '  xlSheet1.Range(GetColumnExcel(rowNew + 2) & startRow, GetColumnExcel((rowNew + colspanColumn) + 2) & startRow).MergeCells = True
                            xlSheet1.Range(GetColumnExcel(rowNew + 2) & startRow, GetColumnExcel((rowNew + colspanColumn) + 2) & startRow).Borders.Weight = 2
                            xlApp.ActiveSheet.Cells(startRow, rowNew + 2).Value = str
                            If Not (DOMESTIC) Then
                                xlApp.ActiveSheet.Cells(startRow, rowNew + 2).Font.Bold = True
                            End If
                            xlApp.ActiveSheet.Cells(startRow, rowNew + 2).Interior.ColorIndex = bgcolorWharf
                        Else
                            ' XHTML &= GenColorTab(dq.Item("Confirm_Time"), colspanColumn + 1, T_From.Split(":")(0), T_To.Split(":")(0), T_From.Split(":")(1), T_To.Split(":")(1), color, dsBooking, Dateshow, T_From, T_To, D_From, D_To, row, Booking)
                            ' xlSheet1.Range(GetColumnExcel(rowNew + 2) & startRow, GetColumnExcel((rowNew + colspanColumn) + 2) & startRow).MergeCells = True
                            xlSheet1.Range(GetColumnExcel(rowNew + 2) & startRow, GetColumnExcel((rowNew + colspanColumn) + 2) & startRow).Borders.Weight = 2
                            xlApp.ActiveSheet.Cells(startRow, rowNew + 2).Value = str
                            If Not (DOMESTIC) Then
                                xlApp.ActiveSheet.Cells(startRow, rowNew + 2).Font.Bold = True
                            End If
                            xlApp.ActiveSheet.Cells(startRow, rowNew + 2).Interior.ColorIndex = bgcolorWharf

                            If (CDate(D_From) < CDate(txtDate.Text)) Then
                                Dim Sub_Day As Integer = DateDiff(DateInterval.Day, CDate(D_From), CDate(txtDate.Text))
                                For a As Integer = 1 To (Day - (colspanColumn + ColumnEmpty)) - Sub_Day
                                    ' XHTML &= "<td align='center' height='24px' style='width:140px;background-color:" & bgcolor & ";' >&nbsp;</td>"
                                    xlSheet1.Range(GetColumnExcel((rowNew + colspanColumn) + (a + 2)) & startRow, GetColumnExcel((rowNew + colspanColumn) + (a + 2)) & startRow).Borders.Weight = 2
                                    xlApp.ActiveSheet.Cells(startRow, (rowNew + colspanColumn) + (a + 2)).Interior.ColorIndex = bgcolorWharf
                                Next
                            Else
                                Dim Loop_For As Integer = 0
                                If (colspanColumn + ColumnEmpty) < 0 Then
                                    Loop_For = Day
                                Else
                                    Loop_For = (Day - (colspanColumn + ColumnEmpty))
                                End If
                                For a As Integer = 1 To Loop_For + 1
                                    'XHTML &= "<td align='center' height='24px' style='width:140px;background-color:" & bgcolor & ";' >&nbsp;</td>"
                                    xlSheet1.Range(GetColumnExcel((ColumnEmpty + colspanColumn) + (a + 1)) & startRow, GetColumnExcel((ColumnEmpty + colspanColumn) + (a + 1)) & startRow).Borders.Weight = 2
                                    xlApp.ActiveSheet.Cells(startRow, (ColumnEmpty + colspanColumn) + (a + 1)).Interior.ColorIndex = bgcolorWharf
                                Next
                            End If
                        End If
                        startRow += 1
                    Next
                Else

                    xlSheet1.Range("A" & startRow, "A" & startRow).Borders.Weight = 2
                    xlApp.ActiveSheet.Cells(startRow, 1).Value = dr.Item("Wharf")
                    xlApp.ActiveSheet.Cells(startRow, 1).Interior.ColorIndex = bgcolorWharf
                    xlSheet1.Range("A" & startRow).HorizontalAlignment = HorizontalAlign.Center
                    xlSheet1.Range("A" & startRow).VerticalAlignment = VerticalAlign.Middle
                    startRow += 1

                    For i As Integer = 0 To Day
                        xlSheet1.Range(GetColumnExcel(i + 2) & startRow - 1, GetColumnExcel(i + 2) & startRow - 1).Borders.Weight = 2
                        xlApp.ActiveSheet.Cells(startRow, i + 2).Interior.ColorIndex = bgcolorWharf
                    Next
                End If
            Next
            startRow += 1
            xlSheet1.Range(GetColumnExcel(Day - 1) & startRow, GetColumnExcel(Day + 1) & startRow).MergeCells = True
            xlApp.ActiveSheet.Cells(startRow, Day - 1).Value = Date.Now.ToString("dd MMM yyyy HH:mm:ss", New CultureInfo("en-US"))
            '*** If Files Already Exist Delete files ***'
            Dim MyFile As New FileInfo(Server.MapPath(FileName))
            If MyFile.Exists Then
                MyFile.Delete()
            End If
            MyFile = Nothing

            Dim strSystemDir As String = Environment.GetEnvironmentVariable("SystemDrive") & "\Temp\"
            Dim DirInfo As New DirectoryInfo(strSystemDir)
            '*** Create Folder ***'
            If Not DirInfo.Exists Then
                DirInfo.Create()
            End If

            '*** Save Excel ***'
            xlSheet1.SaveAs(Server.MapPath(FileName))
            'xlSheet1.SaveAs(strSystemDir & FileName) '*** Save as System drive etc, "C:\Temp"
            xlApp.Quit()
            xlSheet1 = Nothing
            xlBook = Nothing
            xlApp = Nothing

            FileName = GuidID
            Dim FileGen As String = GuidID & ".xls"
            ' Dim FileType As String = gridfile.DataKeys(Index).Values("File_Type")
            Dim ContentType As String = "application/ms-excel"

            'For Test Local Only
            'Dim xlsApp As Excel.Application
            'Dim xlsWB As Excel.Workbook
            'Dim xlsSheet As Excel.Worksheet
            'Dim xlsCell As Excel.Range

            'xlsApp = New Excel.Application
            'xlsApp.Visible = True
            'xlsWB = xlsApp.Workbooks.Open(strSystemDir & FileGen)
            'xlsSheet = xlsWB.Worksheets(1)
            'xlsCell = xlsSheet.Range("A1")

            Dim script As String = "window.open('IPORTDownloadfile.aspx?str2=" + FileGen + "&str3=." + ContentType + "&str4=" + FileName + "','_blank','left=0,top=0,location=no,menubar= no,resizable=yes,scrollbars=no,toolbar=no,status=no,width=100px,height=50px');"
            ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", script, True)
        Catch ex As Exception
            lbError.Text = ex.Message
        End Try

    End Sub

    Public Sub xx()
        Dim xlApp As New Excel.Application
        Dim sh As New Excel.Worksheet
        sh = xlApp.Worksheets
        sh.Range("ww", "33").MergeCells = True
    End Sub

    Private Sub ExportExcel()
        Me.EnableViewState = False
        Try
            System.Threading.Thread.CurrentThread.CurrentCulture = System.Globalization.CultureInfo.CreateSpecificCulture("en-US")

            If ((txtDate.Text.Trim() = "") Or (txtDateTo.Text.Trim() = "")) Then
                ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", " alert('กรุณาทำการระบุช่วงการแสดงผล !!');", True)
                Exit Sub
            End If
            If (CDate(txtDate.Text) > CDate(txtDateTo.Text)) Then
                ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", " alert('ช่วงระยะเวลาการแสดงผลไม่ถูกต้อง!!');", True)
                Exit Sub
            End If

            Dim strSystemDir As String = Environment.GetEnvironmentVariable("SystemDrive") & "\Temp\"
            Dim DirInfo As New DirectoryInfo(strSystemDir)
            '*** Create Folder ***'
            If Not DirInfo.Exists Then
                DirInfo.Create()
            End If
            Dim GuidID As String
            GuidID = Guid.NewGuid.ToString()
            Dim FileName As String = GuidID & ".xls"

            'strHtml = New StringBuilder()
            strHtml1 = New StringBuilder()
            'strHtml.Append("<table  align='center' border='1' bordercolor='#00aeef' width='99%' class='reporttable1' cellspacing='0' cellpadding='0' style='font-size:10;'>")
            'strHtml.Append("<tr>")
            'strHtml.Append("<td>")
            'strHtml.Append("<b>S.No</b>")
            'strHtml.Append("</td>")
            'strHtml.Append("<td>")
            'strHtml.Append("<b>Name</b>")
            'strHtml.Append("</td>")
            'strHtml.Append("</tr>")
            'strHtml.Append("<tr>")
            'strHtml.Append("<td>")
            'strHtml.Append("1")
            'strHtml.Append("</td>")
            'strHtml.Append("<td>")
            'strHtml.Append("Shubhang Mathur")
            'strHtml.Append("</td>")
            'strHtml.Append("</tr>")
            'strHtml.Append("<tr>")
            'strHtml.Append("<td>")
            'strHtml.Append("2")
            'strHtml.Append("</td>")
            'strHtml.Append("<td>")
            'strHtml.Append("Shubhang Sahai Mathur")
            'strHtml.Append("</td>")
            'strHtml.Append("</tr>")
            'strHtml.Append("</table>".ToString())

            strHtml1.Append("<html xmlns:o='urn:schemas-microsoft-com:office:office' xmlns:w='urn:schemas-microsoft-com:office:exc" & _
                        "el' xmlns='http://www.w3.org/TR/REC-html40'><head><title>Time</title>")
            strHtml1.Append("<body lang=EN-US style='mso-element:header' id=h1><span style='mso--code:DATE'></span><div class=Sect" & _
                "ion1>")
            strHtml1.Append("<DIV  style='font-size:12px;'>")
            strHtml1.Append(strHtml.ToString())
            strHtml1.Append("</div></body></html>")

            Dim strcontentType As String = "application/vnd.ms-excel"
            Response.ClearContent()
            Response.ClearHeaders()
            Response.BufferOutput = True
            Response.ContentType = strcontentType
            'file open mode with file name
            Response.AddHeader("Content-Disposition", ("attachment; filename=" + FileName))
            Response.Write(LiWharf.Text.ToString())
            Response.Flush()
            Response.Close()
            Response.End()

        Catch ex As Exception
            lbError.Text = ex.Message
        End Try
    End Sub

    Private Sub btnPrint_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnPrint.Click                
        'ExportExcel()
        GenExcel()
    End Sub

    Public Overrides Sub VerifyRenderingInServerForm(ByVal control As System.Web.UI.Control)
        'MyBase.VerifyRenderingInServerForm(control)
    End Sub
End Class